spring 集成shiro 之 自定义过滤器

出自: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 plain copy
  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.     
  11.    <filter-mapping>  
  12.         <filter-name>shiroFilter</filter-name>  
  13.         <url-pattern>/*</url-pattern>  
  14.    </filter-mapping>  


applicationContext.xml文件中的配置:

[html] view plain copy
  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>    

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

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

[java] view plain copy
  1. public class MonitorRealm extends AuthorizingRealm{  
  2.       
  3.     @Autowired  
  4.     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>  
  5.   
  6.     public MonitorRealm() {  
  7.         super();  
  8.     }  
  9.       
  10.     /** 
  11.      * 授权操作,决定那些角色可以使用那些资源 
  12.      */  
  13.     @Override  
  14.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {  
  15.           
  16.         //<span >TODO </span><span >访问授权信息</span>  
  17.         return info;  
  18.     }  
  19.   
  20.     /** 
  21.      * 认证操作,判断一个请求是否被允许进入系统 
  22.      */  
  23.     @Override  
  24.     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {  
  25.           
  26.         //<span >TODO 用户认证信息  </span>  
  27.             return info;    
  28.     }  
  29.   
  30. }  
[java] view plain copy
  1. public class PermissionsAuthorizationFilter extends AuthorizationFilter {  
  2.   
  3.     public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {  
  4.         <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>     
  5.         return true;  
  6.     }  
  7. }  

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

[html] view plain copy
  1. <property name="filters">    
  2.             <map>    
  3.                 <entry key="<span attribute-value" >background-color: rgb(255, 255, 0);">sperm</span>" value-ref="simplePermFilter"/>  
  4.             </map>    
  5.         </property>    
  6.         <property name="filterChainDefinitions">  
  7.             <value>  
  8.             ...     
  9.                 /test/objT.do = <span style="background-color: rgb(255, 255, 0);">sperm</span>  
  10.             </value>  
  11.         </property>  

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

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

原文地址:https://www.cnblogs.com/onlymate/p/6930205.html