打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
我的shiro之旅: 三 浅谈shiro的filter
WindySky
>《我的shiro之旅》
2017.12.27
关注
前段时间比较懒,项目也有些紧,没有写什么东西。现在再对Shiro做一些整理。上一篇主要介绍了一个完整而又简单的shiro集成到项目的例子,主要是spring项目。这篇文章,想谈一下关于shiro的filter,这需要读者对shiro有一定的理解,至少有用过shiro。
[html]
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/home" />
<property name="unauthorizedUrl" value="/unauthorized" />
<!-- The 'filters' property is usually not necessary unless performing
an override, which we want to do here (make authc point to a PassthruAuthenticationFilter
instead of the default FormAuthenticationFilter: -->
<!-- <property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
</entry>
</map>
</property> -->
<property name="filterChainDefinitions">
<value>
/admin = authc,roles[admin]
/edit = authc,perms[admin:edit]
/home = user
/** = anon
</value>
</property>
</bean>
从上面的配置我们可以看到,当用户没有登录的时候,会重发一个login请求,引导用户去登录。当然,这个login请求做些什么工作,引导用户去那里,完全由开发者决定。successUrl是当用户登录成功,重发home请求,引导用户到主页。unauthorizedUrl指如果请求失败,重发/unauthorized请求,引导用户到认证异常错误页面。
Filter NameClass
anon
org.apache.shiro.web.filter.authc.AnonymousFilter
authc
org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logout
org.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreation
org.apache.shiro.web.filter.session.NoSessionCreationFilter
perms
org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port
org.apache.shiro.web.filter.authz.PortFilter
rest
org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles
org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl
org.apache.shiro.web.filter.authz.SslFilter
user
org.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来改变。如上面的配置
[html]
<span style="white-space:pre;"> </span><property name="filters">
<map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
</entry>
</map>
</property>
把PassThruAuthenticationFilter添加别名为authc,这时/admin请求会交给PassThruAuthenticationFilter处理,替换了原来由
FormAuthenticationFilter
来处理。
由此一来,如果有些特殊的请求需要特殊处理,就可以自己写一个filter,添加一个别名,如:
[html]
<span style="white-space:pre;"> </span><property name="filters">
<map>
<entry key="new">
<bean class="org.xx.xx.NewFilter" />
</entry>
</map>
</property>
请求用/new = new,这样/new请求交由NewFilter来处理。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
在项目中集成shiro权限框架(3)
第六章:Shiro的Realms
Apache Shiro 使用手册(五)Shiro 配置说明
shiro实现不同身份使用不同Realm进行验证
将 Shiro 作为应用的权限基础 四:shiro的配置说明
shiro用authc配置url和用注解@RequiresAuthentication的问题
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×