有两种方式实现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" /> |
16 | < property name = "filterChainDefinitions" > |
18 | /admin = authc,roles[admin] |
19 | /edit = authc,perms[admin:edit] |
从上面的配置我们可以看到,当用户没有登录的时候,会重发一个login请求,引导用户去登录。当然,这个login请求做些什么工作,引导用户去那里,完全由开发者决定。successUrl是当用户登录成功,重发home请求,引导用户到主页。unauthorizedUrl指如果请求失败,重发/unauthorized请求,引导用户到认证异常错误页面。
以上是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" > |
4 | < bean class = "org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /> |
把PassThruAuthenticationFilter添加别名为authc,这时/admin请求会交给PassThruAuthenticationFilter处理,替换了原来由
FormAuthenticationFilter来处理。
由此一来,如果有些特殊的请求需要特殊处理,就可以自己写一个filter,添加一个别名,如:
1 | < span style = "white-space:pre" > </ span >< property name = "filters" > |
4 | < bean class = "org.xx.xx.NewFilter" /> |
请求用/new = new,这样/new请求交由NewFilter来处理。