spring中 shiro logout 配置方式

有两种方式实现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来处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
web.xml配置 因为我们是与spring进行集成的,而spring的基本就是web项目的xml文件。所以我们在web.xml配置shiros的过滤拦截。正常情况下,我们需要将shiro的filter配置在所有的filter前面,当然和encodingFilter这个filter是不区分前后的。因为两者互相不影响的。spring-shiro.xml 这里我们将来看看spring-shiro.xml的配置,这里我采取倒叙的方式讲解,我觉的倒叙更加的有助于我们理解代码。首先我们还记得在web.xml配置的那个filter吧,名字shiroFilter,对spring-shiro.xml配置文件就是通过这个filter展开的。首先我们在web.xml配置的过滤器实际上是配置ShiroFilterFactoryBean,所以在这里需要将ShiroFilterFactoryBean定义为shiroFilter <!-- Shiro的核心安全接口,这个属性是必须的 --> <!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录下的"/login.html"页面 --> <!-- 登录成功后要跳转的连接 --> <!-- 用户访问未对其授权的资源时,所显示的连接 --> <!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp,然后用[玄玉]登录后访问/admin/listUser.jsp就看见浏览器会显示unauthor.jsp --> <!-- Shiro连接约束配置,即过滤链的定义 --> <!-- 此处可配合我的这篇文章来理解各个过滤连的作用http://blog.csdn.net/jadyer/article/details/12172839 --> <!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 --> <!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 --> <!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter --> /statics/**=anon /login.html=anon /sys/schedule.html=perms[sys:schedule:save] /sys/login=anon /captcha.jpg=anon /**=authc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值