2012-08-03 83 views
1

如何添加标准的HTTP过滤器的多个自定义实现一个Spring Security的命名空间的FilterChainProxy?我知道我可以使用after,before或者position属性添加一个过滤器到链中。但是,我怎样才能添加多个过滤器?这是我的安全配置文件添加多个过滤器SpringSecurity命名空间FilterChain

<http pattern="/javax.faces.resource/**" security="none"/> 
<http pattern="/resources/**" security="none"/> 
<http pattern="/session_list.jsp" security="none"/> 
<http pattern="/security/cas_logout.jsf" security="none"/> 
<http pattern="/user/account_signup.jsf" security="none"/> 
<http pattern="/user/company_user_association.jsf" security="none"/> 
<http pattern="/user/account_signup_review.jsf" security="none"/>      

<http auto-config="true" use-expressions="true" entry-point-ref="casEntryPoint" access-decision-manager-ref="accessDecisionManager">    
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/> 
    <logout logout-success-url="/security/cas_logout.jsf" invalidate-session="true"/> 
    <custom-filter position="CAS_FILTER" ref="casFilter"/> 
    <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/> 
    <custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/>  
</http> 

    <beans:bean id="portalSessionFilter" class="org.x.web.security.PortalSessionInterceptor"/>  
    <beans:bean id="requestUrlStackFilter" class="org.x.web.security.RequestUrlStackFilter"/> 

我想给portalSessionFilter和requestUrlStackFilter添加到上面的过滤器链。我有可能做到这一点

<custom-filter ref="portalSessionFilter" before="LAST"/> 

但我怎么前的最后一次,并在之后portalSessionFilter BTW他们两人都是简单GenericFilterBean实现增加requestUrlStackFilter

我试图在其链表上述两个过滤器创建另一个的FilterChainProxy豆和之前的最后添加bean作为一个自定义过滤器的命名空间配置和过滤器似乎工作,但我的JSF导航坏了,特别是与commandLink(我认为AJAX调用使用这个FilterChainProxy会失败时)

任何人都可以提出关于如何将这两个过滤器和其他可能的筛选器添加到Spring Security的过滤器链的任何想法?

+0

正如在这[后](http://stackoverflow.com/questions/3931812/how-to-use-more-than-one-custom-filter-invoked-after-each-other)中提到的,我可以定位后<定制过滤器位置= “LAST” REF = “过滤器3”/> <定制滤波器之前= “LAST” REF = “过滤器2”/> <定制滤波器= “SWITCH_USER_FILTER” REF:只有最多3这样=“filter1”/>,因为SwitchUserFilter是标准顺序中的最后一个。 – Ravi 2012-08-04 01:02:03

回答

2

我找到了我遇到的问题的解决方案。我通过实现我自己的过滤器链来解决它,而不是使用Spring安全筛选器链代理来链接我的过滤器。 FilterChainProxy的问题在于,它在过滤器链的末端使用重置方法,如果将其注入到主弹簧安全筛选器链代理之间,将导致问题。

自定义过滤器链只处理所需的过滤器和双手向后控制主弹簧安全的FilterChainProxy。这里是配置看起来如何的变化

<http auto-config="false" use-expressions="true" entry-point-ref="casEntryPoint" access-decision-manager-ref="accessDecisionManager">   
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/> 
    <logout logout-success-url="/security/cas_logout.jsf" invalidate-session="true"/> 
    <custom-filter position="CAS_FILTER" ref="casFilter"/> 
    <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER"/> 
    <custom-filter ref="singleLogoutFilter" before="CAS_FILTER"/> 
    <custom-filter ref="customFilterChain" before="LAST"/> 
</http> 

<beans:bean id="customFilterChain" class="org.x.web.security.CustomFilterChain"> 
<beans:constructor-arg> 
    <beans:list> 
     <filter-chain pattern="/javax.faces.resource/**" filters="none"/> 
     <filter-chain pattern="/resources/**" filters="none"/> 
     <filter-chain pattern="/**" filters="portalSessionFilter,requestUrlStackFilter"/> 
    </beans:list> 
</beans:constructor-arg> 


这里CustomFilterChain延伸GenericFilterBean之后但在的doFilter方法链过滤器(请求模式匹配)并最终将控制权交还给主弹簧安全FilterChainProxy