2015-09-20 88 views
1

我無法確定Spring安全性在何時何地執行身份驗證管理器。我的意思是有其如下順序執行certian過濾器:Spring安全執行順序

FIRST 
- CHANNEL_FILTER 
- CONCURRENT_SESSION_FILTER 
- SECURITY_CONTEXT_FILTER 
- LOGOUT_FILTER 
- X509_FILTER 

- PRE_AUTH_FILTER 
- CAS_FILTER 
- FORM_LOGIN_FILTER 
- OPENID_FILTER 
- BASIC_AUTH_FILTER 
- SERVLET_API_SUPPORT_FILTER 
- REMEMBER_ME_FILTER 
- ANONYMOUS_FILTER 

- EXCEPTION_TRANSLATION_FILTER 
- SESSION_MANAGEMENT_FILTER 
- FILTER_SECURITY_INTERCEPTOR 
- SWITCH_USER_FILTER 
- LAST 

但當正好驗證提供驗證提供的用戶名和密碼,我的意思是問之後,這些過濾器下面是身份驗證提供被執行。

問候 Jayendra

回答

1

Spring Security documentation

該過濾器在鏈中定義的順序是非常重要的。你實際上是使用 不管採取哪種過濾器,順序應該 如下:

  1. ChannelProcessingFilter,因爲它可能需要重定向到其他協議。

  2. SecurityContextPersistenceFilter,這樣SecurityContext可以設置在web請求的開始處在SecurityContextHolder中,並且當Web請求結束時(準備好用於下一個web請求),對SecurityContext的任何改變可以被複制到HttpSession

  3. ConcurrentSessionFilter,因爲它使用SecurityContextHolder功能,但是需要更新SessionRegistry來 從主要

  4. 認證處理機制進行的請求 - UsernamePasswordAuthenticationFilter,CasAuthenticationFilter, BasicAuthenticationFilter一樣等等 - 這樣SecurityContextHolder可以 是修改爲包含有效的身份驗證請求令牌

  5. The SecurityContextHolderAwareRequestFilter,如果您使用它來安裝支持Spring Security的Http了ServletRequestWrapper到您的 servlet容器

  6. RememberMeAuthenticationFilter,這樣如果之前的驗證執行機制沒有更新SecurityContextHolder, 這個請求提供一個cookie,使記得,我 採取到位的服務,合適的記憶驗證的對象將是把 有

  7. AnonymousAuthenticationFilter,這樣如果之前的驗證執行機制沒有更新SecurityContextHolder, 一個匿名Authentication對象會被放在那裏

  8. ExceptionTranslationFilter,用來捕捉Spring Security異常,這樣要麼一個HTTP錯誤響應可以返回或 適當的AuthenticationEntryPoint。

  9. FilterSecurityInterceptor,以保護網絡的URI,當訪問被拒絕引發異常

所以認證管理器在步驟4名爲如果你看看UsernamePasswordAuthenticationFilter的源代碼,你會看到類似這樣的:

public Authentication attemptAuthentication(HttpServletRequest request, 
     HttpServletResponse response) throws AuthenticationException { 
    // ... 
    return this.getAuthenticationManager().authenticate(authRequest); 
} 
+1

非常真實。在實現一個需要注入認證管理器的自定義用戶名密碼驗證過濾器時實現了這一點。 –