ITeye博客

spring 集成shiro 之 自定义过滤器shiroFilter

coffeehot · 2015-11-26 13:46 · spring · shiro · 企业架构 · Markdown

转:http://blog.csdn.net/shuishouhcd/article/details/9077379

 

最近一段时间,我一直在将shiro集成到我的一个项目中,用作认证和授权处理。

 

            shiro对我来说是个新东西,以下是我学习过的内容:

 

            http://shiro.apache.org/authorization.html

            http://www.cnblogs.com/skyme/archive/2011/09/11/2173760.html  系列

            http://www.infoq.com/cn/articles/apache-shiro

            http://kdboy.iteye.com/blog/1103794

http://www.ibm.com/developerworks/cn/java/j-lo-shiro/

 

如果我那个地方没说明白,可以看这些。

 集成shiro,需要配置web.xml文件,spring的applicationContext.xml配置文件(当然,独立配置一个shiro.xml文件交给spring容器处理也是可以的)。

web.xml文件中的配置:

[html] view plaincopy

 

  1. <!-- shiro filter的名字是shiroFilter,那么在spring的配置文件中要有一个名字为shiroFilter的bean-->
  2. <filter>
  3. <filter-name>shiroFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  5. <init-param>
  6. <param-name>targetFilterLifecycle</param-name>
  7. <param-value>true</param-value>
  8. </init-param>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>shiroFilter</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>

applicationContext.xml文件中的配置:

 

[html] view plaincopy

 

  1. <!-- 自定义的Shiro Filter-->
  2. <bean id="simplePermFilter" class="frame.security.PermissionsAuthorizationFilter"></bean>
  3. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  4. <property name="securityManager" ref="securityManager" />
  5. <property name="loginUrl" value="/login" />
  6. <property name="successUrl" value="/user/list" />
  7. <property name="unauthorizedUrl" value="/login" />
  8. <property name="filters">
  9. <map>
  10. <entry key="sperm" value-ref="simplePermFilter"/>
  11. </map>
  12. </property>
  13. <property name="filterChainDefinitions">
  14. <value>
  15. /Flat-UI-master/**=anon
  16. /index.jsp* = anon
  17. /test.jsp*=anon
  18. /jsp/** = authc
  19. /test/objT.do = sperm
  20. </value>
  21. </property>
  22. </bean>
  23. <!--设置自定义realm -->
  24. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  25. <property name="realm" ref="myRealm" />
  26. </bean>
  27. <!-- 配置shiro bean processor-->
  28. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
  29. <!--myRealm 继承自AuthorizingRealm-->
  30. <bean id="myRealm" class="frame.security.MonitorRealm" ></bean>
  31. <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  32. <property name="staticMethod"
  33. value="org.apache.shiro.SecurityUtils.setSecurityManager" />
  34. <property name="arguments" ref="securityManager" />
  35. </bean>

 

 

代码说明:

  1. shiroFilter 中 loginUrl 为登录页面地址,successUrl 为登录成功页面地址(如果首先访问受保护 URL 登录成功,则跳转到实际访问页面),unauthorizedUrl 认证未通过访问的页面(前面提到的“未经授权页面”)。
  2. shiroFilter 中 filters 属性,formAuthenticationFilter 配置为基于表单认证的过滤器。
  3. shiroFilter 中 filterChainDefinitions 属性,anon 表示匿名访问(不需要认证与授权),authc 表示需要认证,perms[SECURITY_ACCOUNT_VIEW] 表示用户需要提供值为“SECURITY_ACCOUNT_VIEW”Permission 信息。由此可见,连接地址配置为 authc 或 perms[XXX] 表示为受保护资源。
  4. securityManager 中 realm 属性,配置为我们自己实现的 Realm。关于 Realm,参见前面“Shiro Realm”章节。
  5. myShiroRealm 为我们自己需要实现的 Realm 类,为了减小数据库压力,添加了缓存机制。
  6. shiroCacheManager 是 Shiro 对缓存框架 EhCache 的配置。

MonitorRealm.java 是自定义的realm,读取数据库的用户信息,和授权信息。

 

 PermissionsAuthorizationFilter.java 是自定义的过滤器,来实现自己需要的授权过滤方式。

[java] view plaincopy

 

  1. public class MonitorRealm extends AuthorizingRealm{
  2. @Autowired
  3. private DAO dao;<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; line-height: 17.27272605895996px; background-color: rgb(248, 248, 248);">//这里自己需要什么就注入什么。  </span>
  4. public MonitorRealm() {
  5. super();
  6. }
  7. /**
  8. * 授权操作,决定那些角色可以使用那些资源
  9. */
  10. @Override
  11. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
  12. //<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; line-height: 17.27272605895996px; background-color: rgb(248, 248, 248);">TODO </span><span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; line-height: 17.27272605895996px; background-color: rgb(248, 248, 248);">访问授权信息</span>
  13. return info;
  14. }
  15. /**
  16. * 认证操作,判断一个请求是否被允许进入系统
  17. */
  18. @Override
  19. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
  20. //<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; line-height: 17.27272605895996px;">TODO 用户认证信息  </span>
  21. return info;
  22. }
  23. }

 

[java] view plaincopy

 

  1. public class PermissionsAuthorizationFilter extends AuthorizationFilter {
  2. public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
  3. <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; line-height: 17.27272605895996px; background-color: rgb(248, 248, 248);"> //自定义过滤器逻辑</span>
  4. return true;
  5. }
  6. }

配置自定义过滤器的关键是配置文件中 的这几句

 

[html] view plaincopy

 

  1. <property name="filters">
  2. <map>
  3. <entry key="sperm" value-ref="simplePermFilter"/>
  4. </map>
  5. </property>
  6. <property name="filterChainDefinitions">
  7. <value>
  8. ...
  9. /test/objT.do ="sperm"
  10. </value>
  11. </property>

颜色相同的地方一定要一样,表示用某个过滤器过滤指定路径。因为这个我费了好长时间。

 

 

org.apache.shiro.spring.web.ShiroFilterFactoryBean   的作用是通过spring来初始化shiro的工作环境。如果一个请求进来,shiro的过滤器会先工作,过滤器会调用realm中的授权或认证的方法来获取授权或认证信息。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。