shiro是一个非常强大灵活的权限控制框架,属于apache的顶级项目.springrain使用shiro实现了权限控制功能.
下图充分说明了shiro的体系架构

归根到底,权限控制无非是利用过滤器控制访问的认证和授权,shiro也不例外.我们来看看shiro是怎么实现的吧.
要在web中使用shiro,总共分三步:
第一步:在web.xml中配置shiro的过滤器,建议是应用的第一个过滤器,springrain示例配置如下:
02 |
< filter-name >shiroFilter</ filter-name >
|
03 |
< filter-class >org.springframework.web.filter.DelegatingFilterProxy</ filter-class >
|
05 |
< param-name >targetFilterLifecycle</ param-name >
|
06 |
< param-value >true</ param-value >
|
10 |
< filter-name >shiroFilter</ filter-name >
|
11 |
< url-pattern >/*</ url-pattern >
|
12 |
< dispatcher >REQUEST</ dispatcher >
|
13 |
< dispatcher >FORWARD</ dispatcher >
|
14 |
< dispatcher >INCLUDE</ dispatcher >
|
15 |
< dispatcher >ERROR</ dispatcher >
|
这个shiroFilter其实是个spring bean,等下会重点说这个bean,dispatcher这个标签是为了在forward和redirect的情况下也需要经过过滤器
第二步:配置spring-shiro,springrain配置是applicationContext-shiro.xml
02 |
< bean id = "securityManager" class = "org.apache.shiro.web.mgt.DefaultWebSecurityManager" >
|
04 |
< property name = "realm" ref = "shiroDbRealm" />
|
06 |
< property name = "sessionManager" ref = "sessionManager" />
|
08 |
< property name = "cacheManager" ref = "shiroCacheManager" />
|
11 |
< bean id = "sessionManager" class = "org.apache.shiro.web.session.mgt.DefaultWebSessionManager" >
|
13 |
< property name = "globalSessionTimeout" value = "1800000" />
|
15 |
< property name = "sessionDAO" ref = "shiroSessionDao" />
|
17 |
< property name = "sessionIdCookie" ref = "sharesession" />
|
19 |
< property name = "sessionValidationSchedulerEnabled" value = "true" />
|
23 |
< bean id = "sharesession" class = "org.apache.shiro.web.servlet.SimpleCookie" >
|
25 |
< constructor-arg name = "name" value = "SHAREJSESSIONID" />
|
28 |
< bean id = "shiroSessionDao" class = "org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO" />
|
31 |
< bean id = "shiroCacheManager" class = "org.apache.shiro.cache.MemoryConstrainedCacheManager" />
|
34 |
< bean id = "shiroFilter" class = "org.apache.shiro.spring.web.ShiroFilterFactoryBean"
|
35 |
depends-on = "frameperms" >
|
37 |
< property name = "securityManager" ref = "securityManager" />
|
39 |
< property name = "loginUrl" value = "/login" />
|
41 |
< property name = "successUrl" value = "/index" />
|
43 |
< property name = "unauthorizedUrl" value = "/unauth" />
|
45 |
< property name = "filterChainDefinitions" >
|
63 |
< property name = "filters" >
|
65 |
< entry key = "frameperms" value-ref = "frameperms" ></ entry >
|
70 |
< bean id = "lifecycleBeanPostProcessor" class = "org.apache.shiro.spring.LifecycleBeanPostProcessor" />
|
frameperms是自定义的过滤器,除了那些特殊url,其他的菜单都在数据库,查询用户权限判断,下一篇文章介绍下权限相关的表结构.
另外不建议在web项目使用类似 admin:user:edit这种方式控制权限,这种方式等同给url又起了一个别名,这样虽然看起来比较容易理解,但是相当死板和麻烦.建议直接使用url判断权限
authc 和 user的区别是 user包含 rememberme,authc不包含,就这一点区别.
第三步:实现数据库认证和权限过滤
数据库认证shiroDbRealm的代码:
http://git.oschina.net/chunanyong/springrain/blob/master/springrain/src/org/springrain/frame/shiro/ShiroDbRealm.java
自定义权限过滤frameperms的代码:
http://git.oschina.net/chunanyong/springrain/blob/master/springrain/src/org/springrain/frame/shiro/FramePermissionsAuthorizationFilter.java
另外 springrain 没有使用authc实现登陆,而是使用一个普通的controller方法进行登陆
org.springrain.frame.controller.BaseController.loginPost(User, HttpSession, Model, HttpServletRequest)
其中
//会调用 shiroDbRealm 的认证方法
//org.springrain.frame.shiro.ShiroDbRealm.doGetAuthenticationInfo(AuthenticationToken)
user.login(token);
本文出自 9iu.org,转载时请注明出处及相应链接。
本文永久链接: http://www.9iu.org/2013/12/10/springrain1-shiro.html