您的位置:首页 > 编程语言 > Java开发

spring中 shiro logout 配置方式

2016-08-05 14:56 260 查看
有两种方式实现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
Name
Class
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
<
bean
class
=
"org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"
/>
5
</
entry
>
6
</
map
>
7
</
property
>
把PassThruAuthenticationFilter添加别名为authc,这时/admin请求会交给PassThruAuthenticationFilter处理,替换了原来由FormAuthenticationFilter来处理。

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

view
source

print?

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来处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shiro