2016-12-29 57 views
1

我想限制第二次會話,而同一用戶嘗試從不同的計算機登錄。 以下是我的Spring Security代碼:限制併發會話並使用Spring Security向用戶發送正確的消息

<form-login authentication-success-handler-ref="myAuthSuccessHandler" 
     login-page='/login' username-parameter="username" 
     password-parameter="password" authentication-failure-url="/loginfailed"/> 

<logout logout-success-url="/logout" delete-cookies="JSESSIONID,the_cookie"/> 

<session-management> 
    <concurrency-control session-registry-alias="sessionRegistry" max-sessions="1"/> 
</session-management> 

現在使用這個我可以限制併發會話,但我不能給予適當的信息給用戶,如:「USER ALREADY登錄不同機」。

回答

0

實現或擴展身份驗證失敗處理程序,例如, SimpleUrlAuthenticationFailureHandler

public class CustomFailureHandler extends SimpleUrlAuthenticationFailureHandler { 

@Override 
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, 
     AuthenticationException exception) throws IOException, ServletException { 
    setDefaultFailureUrl("/loginfailed.jsp?status=" + "YOUR CUSTOM MESSAGE HERE"); 
    super.onAuthenticationFailure(request, response, exception); 
} 
} 
+0

這個解決我的問題。但現在我面臨着一個問題,如果用戶關閉瀏覽器或關閉電腦而不註銷,則同一用戶無法從任何其他機器登錄。用戶需要從同一臺機器登錄。任何幫助將非常感激。 –

+0

在客戶端捕獲窗口的卸載事件,並從那裏點擊註銷URL。所以每次用戶關閉瀏覽器時,他都會先註銷。 –

相關問題