spring中 shiro logout 配置方式

本文介绍了Apache Shiro框架中两种实现用户登出的方法,并详细解析了如何通过配置自定义过滤器来实现特定请求的处理。
有两种方式实现logout
1. 普通的action中 实现自己的logout方法,取到Subject,然后logout
这种需要在ShiroFilterFactoryBean 中配置 filterChainDefinitions 
对应的action的url为anon
<property name="filterChainDefinitions">
            <value>
                # some example chain definitions:
                /index.htm = anon
                /logout = anon
                /unauthed = anon
                /console/** = anon
                /css/** = anon
                /js/** = anon
                /lib/** = anon
                /admin/** = authc, roles[admin]
                /docs/** = authc, perms[document:read]
                /** = authc
                # more URL-to-FilterChain definitions here
            </value>

2. 使用shiro提供的logout filter
需要定义 相应的bean
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
        <property name="redirectUrl" value="/loginform" />
    </bean>

然后将相应的url filter配置为logout如下
<property name="filterChainDefinitions">
            <value>
                # some example chain definitions:
                /index.htm = anon
                 /logout = logout
                /unauthed = anon
                /console/** = anon
                /css/** = anon
                /js/** = anon
                /lib/** = anon
                /admin/** = authc, roles[admin]
                /docs/** = authc, perms[document:read]
                /** = authc
                # more URL-to-FilterChain definitions here
            </value>

注:anon,authcBasic,auchc,user是认证过滤器,perms,roles,ssl,rest,port是授权过滤器


关于自定义filter

我的shiro之旅: 三 浅谈shiro的filter

分类: shiro  |  标签: shiro  |  作者: lhacker  相关  |  发布日期 : 2014-11-29  |  热度 : 63°

前段时间比较懒,项目也有些紧,没有写什么东西。现在再对Shiro做一些整理。上一篇主要介绍了一个完整而又简单的shiro集成到项目的例子,主要是spring项目。这篇文章,想谈一下关于shiro的filter,这需要读者对shiro有一定的理解,至少有用过shiro。

01 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
02         <property name="securityManager" ref="securityManager" />
03         <property name="loginUrl" value="/login" />
04         <property name="successUrl" value="/home" />
05         <property name="unauthorizedUrl" value="/unauthorized" />
06         <!-- The 'filters' property is usually not necessary unless performing
07             an override, which we want to do here (make authc point to a PassthruAuthenticationFilter
08             instead of the default FormAuthenticationFilter: -->
09         <!-- <property name="filters">
10             <map>
11                 <entry key="authc">
12                     <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
13                 </entry>
14             </map>
15         </property> -->
16         <property name="filterChainDefinitions">
17             <value>
18                 /admin = authc,roles[admin]
19                 /edit = authc,perms[admin:edit]
20                 /home = user
21                 /** = anon
22             </value>
23         </property>
24     </bean>

从上面的配置我们可以看到,当用户没有登录的时候,会重发一个login请求,引导用户去登录。当然,这个login请求做些什么工作,引导用户去那里,完全由开发者决定。successUrl是当用户登录成功,重发home请求,引导用户到主页。unauthorizedUrl指如果请求失败,重发/unauthorized请求,引导用户到认证异常错误页面。

Filter NameClass
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logoutorg.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreationorg.apache.shiro.web.filter.session.NoSessionCreationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter

以上是shiro的一些Filter,如我们在filterChainDefinitions里配置了/admin=authc,roles[admin],那么/admin这个请求会由 org.apache.shiro.web.filter.authc.FormAuthenticationFilter和  org.apache.shiro.web.filter.authz.RolesAuthorizationFilter这两个filter来处理,其中authc,roles只是filter的别名。如要更改别名,可以通过filters来改变。如上面的配置 

1 <span style="white-space:pre">      </span><property name="filters">
2             <map>
3                 <entry key="authc">
4                     <beanclass="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
5                 </entry>
6             </map>
7         </property>
把PassThruAuthenticationFilter添加别名为authc,这时/admin请求会交给PassThruAuthenticationFilter处理,替换了原来由 FormAuthenticationFilter来处理。

由此一来,如果有些特殊的请求需要特殊处理,就可以自己写一个filter,添加一个别名,如:

1 <span style="white-space:pre">      </span><property name="filters">
2             <map>
3                 <entry key="new">
4                     <bean class="org.xx.xx.NewFilter" />
5                 </entry>
6             </map>
7         </property>
请求用/new = new,这样/new请求交由NewFilter来处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值