2013-08-29 95 views
0

在我的應用程序中,我有三種方式登錄用戶 表單登錄, 自動登錄和 記住我的cookie。onAuthenticationSuccess沒有被調用

我必須在成功驗證用戶後攔截呼叫,爲此我使用AuthenticationSuccessHandler來攔截呼叫。

我正在使用spring security來驗證用戶身份。對於基於表單的登錄,我已經配置了下面的代碼在彈簧security.xml文件

form-login login-page="/login/formlogin" default-target-url="/login/user_login" 
       authentication-failure-url="/login/loginfailed" 
authentication-success-handler-ref="authenticationSuccessHandlerImpl"/> 

AuthenticationSuccessHandler.onAuthenticationSuccess(); 

認證成功完成後立即被調用,但對於 這種自動登錄的方法是永遠調用。

對於自動登錄用戶將郵寄一個URL,點擊URL將驗證用戶,我正在以編程方式進行。在URL中,我將加密的用戶名(無密碼)。 我使用的代碼下面的線,當他點擊的URL(自動登錄)

Authentication authentication = new UsernamePasswordAuthenticationToken(userName, null, userDetails.getAuthorities()); 
SecurityContextHolder.getContext().setAuthentication(authentication); 

驗證用戶但是,

AuthenticationSuccessHandler.onAuthenticationSuccess() 

在這種情況下不會被調用。

是否有任何其他方式來攔截成功認證的呼叫。

回答

1

對於這兩種情況,您都可以重用已存在的auth成功處理程序。與rememberMe - 只是它配置:

<remember-me authentication-success-handler-ref="authenticationSuccessHandlerImpl"/> 

自動登錄 - 注入相同的身份驗證成功處理程序豆和手動代碼後調用它:

Authentication authentication = new UsernamePasswordAuthenticationToken(userName, null, userDetails.getAuthorities()); 
SecurityContextHolder.getContext().setAuthentication(authentication); 
successHandler.onAuthenticationSuccess(request, response, authentication); 
+0

謝謝,它的工作的罰款。 – user2677325

+0

你好。 –