Spring DelegatingFilterProxy 过滤器 的原理及运用

DelegatingFilterProxy的原理及使用

DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现。

DelegatingFilterProxy的使用方法,

 

首先在web.xml中配置:

如果要保留Filter原有的init,destroy方法的调用,还需要配置初始化参数targetFilterLifecycle为true,该参数默认为false

  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

然后在Spring的配置文件中,配置具体的Filter类的实例(此处以Shiro为例)。

  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- Shiro的核心安全接口,这个属性是必须的 -->
    <property name="securityManager" ref="securityManager"></property>
    <!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->
    <property name="loginUrl" value="/login"></property>
    <property name="filters">
      <map>
        <entry key="anyRole" value-ref="anyRoleAuthorizationFilter" />
      </map>
    </property>
    <!-- 登录成功后要跳转的连接 -->
    <!-- <property name="successUrl" value="/main" ></property> -->
    <!-- 用户访问未对其授权的资源时,所显示的连接 -->
    <property name="unauthorizedUrl" value="/noAuthority"></property>
    <property name="filterChainDefinitionMap" ref="filterChainFactoryBean"></property>
  </bean>

在Spring中配置的bean的name要和web.xml中的<filter-name>一样

或者在DelegatingFilterProxy的filter配置中配置初始参数:targetBeanName,对应到Spring配置中的beanname

    <filter>
        <filter-name>DelegatingFilterProxy</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
              <param-name>ignoreUrls</param-name>
              <param-value>/caClient.jsp,/403.jsp,/login.html*,/j_spring_security_check,/accounts/**,/secure/logout,/js/**,/app/**,/images/**,/icons/**,/lib/**,/resource/**,/secure/changepassword</param-value>
        </init-param>        
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>commonWebFilter</param-value>
        </init-param>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>DelegatingFilterProxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

applicationContext.xml

<bean id="commonWebFilter" class="com.wonders.LoginFilter"/>

 

posted @ 2019-03-02 10:20  高木子  阅读(600)  评论(0编辑  收藏  举报