Spring 的 org.springframework.web.filter.DelegatingFilterProxy

一、org.springframework.web.filter.DelegatingFilterProxy(委托过滤器代理)

      使用springSecurity或apache shiro就会用到这个过滤器,如在web.xml中进行配置

<!-- 配置security的过滤器 -->
	<filter>
	   <filter-name>shiroFilter</filter-name>
	   <filter-class>
	      org.springframework.web.filter.DelegatingFilterProxy
	   </filter-class>
	</filter>
	
	<filter-mapping>
	   <filter-name>shiroFilter</filter-name>
	   <url-pattern>/*</url-pattern>
	</filter-mapping>

从这个配置中,可能会给我们造成一个错觉,以为DelegatingFilterProxy类就是springSecurity的入口,

但其实这个类位于spring-web-3.0.5.RELEASE.jar这个jar下面,说明这个类本身是和springSecurity无关。

DelegatingFilterProxy类继承于抽象类GenericFilterBean,间接地implement 了javax.servlet.Filter接口,

Servlet容器在启动时,首先会调用Filter的init方法,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。


此时需要配合  filter-name    shiroFilter  在spring.xml中进行配置

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
   <property name="securityManager" ref="securityManager"/> 
   <property name="loginUrl" value="/login.do"/> 
   <property name="successUrl" value="/welcome.do"/> 
   <property name="unauthorizedUrl" value="/403.do"/> 
   <property name="filters"> 
      <util:map> 
         <entry key="authc" value-ref="formAuthenticationFilter"/> 
      </util:map> 
   </property> 
   <property name="filterChainDefinitions"> 
      <value> 
         /=anon 
         /login.do*=authc 
         /logout.do*=anon 
         
         # 权限配置示例
         /security/account/view.do=authc,perms[SECURITY_ACCOUNT_VIEW] 
         
         /** = authc 
      </value> 
   </property> 
 </bean> 

 <bean id="securityManager" 
   class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
   <property name="realm" ref="myShiroRealm"/> 
 </bean> 

 <bean id="myShiroRealm" class="xxx.packagename.MyShiroRealm"> 
   <!-- businessManager 用来实现用户名密码的查询 --> 
   <property name="businessManager" ref="businessManager"/> 
   <property name="cacheManager" ref="shiroCacheManager"/> 
 </bean> 

 <bean id="lifecycleBeanPostProcessor" 
    class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 

 <bean id="shiroCacheManager" 
   class="org.apache.shiro.cache.ehcache.EhCacheManager"> 
   <property name="cacheManager" ref="cacheManager"/> 
 </bean> 

 <bean id="formAuthenticationFilter" 
   class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"/>

代码说明:

  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 的配置。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值