2015-09-30 27 views
1

我的公司Intranet應用程序使用預認證方案。它在SpringSecurity過濾器鏈之前插入了兩個過濾器。第一種是公司提供的過濾器。它處理所有登錄,密碼等,如果它識別用戶,則將身份驗證數據放入Cookie中的Principal中。第二個翻譯所有這些,創建UserDetails對象和一個身份驗證令牌並將其放置在SecurityContextHolder中。SpringSecurity爲什麼不把我的SecurityContext放在HttpSession中?

SecurityContextHolder.getContext().setAuthentication(token); 
logger.debug("Auth token submitted"); 

我的日誌證實,這種情況正在發生:

2015-09-30 13:02:08,998 DEBUG c.a.v.c.s.MyPreauthFilter [http-bio-8081-exec-63] Auth token submitted 

數毫秒後,Spring Security的過濾器鏈進來並執行以下操作:

2015-09-30 13:02:09,002 DEBUG o.s.s.w.FilterChainProxy [http-bio-8081-exec-63] /index.html at position 1 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter' 
2015-09-30 13:02:09,007 DEBUG o.s.s.w.FilterChainProxy [http-bio-8081-exec-63] /index.html at position 2 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
2015-09-30 13:02:09,008 DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository [http-bio-8081-exec-63] HttpSession returned null object for SPRING_SECURITY_CONTEXT 
2015-09-30 13:02:09,008 DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository [http-bio-8081-exec-63] No SecurityContext was available from the HttpSession: [email protected] A new one will be created. 

基本上,HttpSessionSecurityContextRespositorySecurityContextPersistenceFilter(過濾鏈的一部分)調用,並檢查SESSION中的安全上下文,在會話中找不到一個,並替換上下文I只是放置在SecurityContextHolder中,一個新的空的,導致下面幾行認證失敗。

2015-09-30 13:02:09,024 DEBUG o.s.s.w.a.ExceptionTranslationFilter [http-bio-8081-exec-63] Access is denied (user is anonymous); redirecting to authentication entry point 
org.springframework.security.access.AccessDeniedException: Access is denied 

由於春節文檔不提HttpSessionSecurityContextRespository,我想我可能是不應該亂用那個。

相反,我想也許我可以嘗試在Spring Security Filter鏈後插入第二個過濾器,但這並沒有幫助。 FilterSecurityInterceptor(鏈中的第11項)拒絕對我進行身份驗證,就像我的第二個篩選器位於鏈中之前那樣。

什麼會保存會話中的SecurityContext,以及如何在清除剛剛設置的安全上下文時打敗SecurityContextPersistenceFilter的行爲?

回答

0

我開發了一個解決方法。它不應該是必要的,但它似乎工作。

@Override 
public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain chain) throws IOException, ServletException 
{ 

... 
    PreAuthenticatedAuthenticationToken token = ... 


    logger.debug("Auth token placed in SecurityContext: \n" + token); 
    SecurityContextHolder.getContext().setAuthentication(token); 

    // make sure the session has a SecurityContext at this point 
    ensureSessionHasSecurityContext(hreq); 

    super.doFilter(request,response, chain); 

    logger.debug("Auth token after rest of chain: \n" + SecurityContextHolder.getContext() 
      .getAuthentication()); 

} 

private void ensureSessionHasSecurityContext(HttpServletRequest hreq) { 
    HttpSession session = hreq.getSession(false); 
    Object securityContext = session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); 
    if (securityContext == null) { 
     logger.debug("no SecurityContext found in session, inserting ours"); 
     session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext()); 
    } 

} 

請對這個解決方案進行討論,如果它出現問題。但這是我能想到的唯一方法,以確保篩選器鏈在我的預認證過濾器完成其工作之後,但在Spring Filter Chain接管之前不會影響我的新認證。

相關問題