0
  1. 我有一個導出web服務的應用程序,其中配置了Spring Security SecurityFilterChain(其中SecurityContextPersistenceFilter等,其餘爲其他需要)。
  2. 我的應用程序還使用Spring Security來保護方法調用。

我有以下時,方法的安全性被觸發錯誤:在SecurityContext中找不到驗證對象

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext 

第二部分要求SecurityContextHolderAuthenticationorg.springframework.security.access.intercept.AbstractSecurityInterceptor(線195)顯示:

SecurityContextHolder.getContext().getAuthentication(); 

但是,SecurityContextPersistenceFilter在觸發方法調用之前將其刪除,如 org.springframework.security.web.context.SecurityContextPersistenceFilter(第84行)

SecurityContextHolder.clearContext(); 

當方法調用被觸發時,我該怎麼做這個對象在SecurityContextHolder

預先感謝您。

我使用Spring Security的3.0.8-RELEASE

回答

0

好的,我的應用程序被放在Apache CXF DOSGi 1.4上以生成REST端點。 Apache CXF攔截器會導致意外的行爲,並在完成請求處理之前調用SecurityContextHolder.clearContext()

有關此錯誤的更多信息,請參見here

3

SecurityContextHolder.clearContext()後,才請求處理完成調用。所以通常你所有的應用程序邏輯代碼將在之前執行這一行,並且根本沒有問題。但是如果你在你的代碼中執行了一些新線程(默認安全上下文不會被傳播),那麼問題就可能出現。如果這是你的情況,那麼你可以try to force context propogation to child thread。如果您只使用一個線程,那麼請確保您的所有代碼都被Spring安全篩選器鏈覆蓋(可能您有一些自定義篩選器執行彈簧安全篩選器鏈?)。