2013-05-27 66 views
6

對於一個項目,我嘗試使用Spring Security 3.2作爲基本安全。因爲這個項目已經啓動並且正在運行,我已經有了其他(自己的)安全層。因此,我做了一個自定義身份驗證提供程序來融合安全層。工作正常,直到我還需要進行自定義匿名身份驗證(Spring Security Documentation, chapter 13)。自定義身份驗證過濾器Spring Security 3.2

所以我做了一個自定義過濾器和刪除的一部開拓創新的過濾器:

<http request-matcher="regex" use-expressions="true"> 
    <anonymous enabled="false" /> 
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/> 
    ... 
</http> 

豆:

<beans:bean id="anonymousAuthFilter" class="own.package.auth.SecurityAnonymousAuthenticationFilter"> 
    <beans:property name="key" value="anonymousKey "/> 
    <beans:property name="userAttribute" value="anonymous,ROLE_ANONYMOUS"/> 
</beans:bean> 

和Te的Java類:

public class SecurityAnonymousAuthenticationFilter extends GenericFilterBean implements InitializingBean { 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     logger.info("Entering doFilter method"); 
     //implementation code here 
    } 

    //other methods 
} 

的問題是,請求服務器時不調用doFilter方法。然而init方法afterPropertiesSet()被調用...有誰明白爲什麼我的customFilter沒有被解僱?

P.S.我確實在web.xml文件中命名了delegatingFilterProxy,所以這不是問題。

+0

嘗試將自定義過濾器注入某個其他位置(例如** after =「ANONYMOUS_FILTER」**)。可能是** anonymous enabled =「false」**通過過濾器位置工作? –

+0

@MaksymDemidas:Thx的評論,不幸的是它不會改變任何東西。 –

+0

你試過刪除** anonymous enabled =「false」**? –

回答

1

由於ANONYMOUS_FILTER是一個與名稱空間相關的過濾器。你必須避免任何命名空間標記,具體過濾psoition引用:

<http auto-config='false' request-matcher="regex" use-expressions="true"> 
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER"/> 
    ... 
    </http> 

有關進一步的參考看到Spring Security的單證在第2.3.5:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html

編輯:而且可以肯定離開<anonymous-enabled=false/>標籤。

編輯2:更正了我的答案。這個配置應該工作。如果沒有,比我們需要開始查看更大的圖片,並且您必須發佈更多的應用程序,從完整的配置開始。

+0

如果未定義,auto-config已經處於'false'狀態。從文檔:_ ** auto-config:**自動註冊登錄表單,BASIC認證,匿名認證,註銷服務,記住我和servlet-api-集成。如果設置爲「true」,則會添加所有這些功能(儘管您仍然可以通過提供相應的元素來自定義每個功能的配置)。如果未指定,則默認爲「false」._ 如前所述,'anonymous-enabled = false'可能不起作用。 –

+0

更新了我的答案。 – Carsten

+0

你說得對。還有一個問題引發了它。我還有一個security = none的其他http標記,可以讓任何人看到一些css + js內容。不幸的是,這種過濾模式是錯誤的,我的整個應用程序不再通過我的匿名過濾器。 Thx和我一起思考! –

相關問題