過濾器Filter中注入類Bean

DelegatingFilterProxy管理過濾器

DelegatingFilterProxy提供了在 web.xml和application context之間的聯繫

FilterChainProxy過濾器鏈

這是一種繁瑣的方式,會讓web.xml顯得十分雜亂,如果我們配置了太多過濾器的話。 我們最好添加一個單獨的入口,在web.xml中,然後在application context中處理實體, 管理我們的web安全bean。 這就是FilterChainProxy所做的事情。


web.xml配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/character-context.xml
            /WEB-INF/spring-configuration/*.xml
            /WEB-INF/deployerConfigContext.xml
           
            classpath*:/META-INF/spring/*.xml
        </param-value>
  </context-param>
  
  <listener>   
        <listener-class>   
            org.springframework.web.context.ContextLoaderListener   
        </listener-class>   
    </listener>  

<filter> 
  <filter-name>DelegatingFilterProxy</filter-name> 
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
  <init-param> 
  <param-name>targetBeanName</param-name>
    <param-value>myFilter</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>


character-context.xml配置

<bean id="cpc" class="com.chinamobile.cmss.sso.web.filter.CustomizedPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/cas.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>

<bean id="myFilter" class="com.chinamobile.cmss.sso.web.filter.CrossDomainFilter">  原始的過濾器,繼承Filter
<property name="cpc">
<ref bean="cpc"/>
</property>
</bean>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章