1

私のアプリケーションでは、ユーザー フォーム ログイン、自動ログイン、および Cookie を記憶する 3 つの方法があります。

User の認証が成功した後に呼び出しを傍受する必要があります。そのため、 AuthenticationSuccessHandlerを使用して呼び出しを傍受しています。

私は春のセキュリティを使用してユーザーを認証しています。フォームベースのログインの場合、spring-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()

この場合、呼び出されることはありません。

認証が成功したときに呼び出しを傍受する他の方法はありますか。

4

1 に答える 1

1

どちらの場合も、既存の認証成功ハンドラーを再利用できます。RememberMe - 構成するだけです:

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

自動ログイン - 同じ認証成功ハンドラー Bean を注入し、コードの後に​​手動で呼び出します。

Authentication authentication = new UsernamePasswordAuthenticationToken(userName, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
successHandler.onAuthenticationSuccess(request, response, authentication);
于 2013-08-29T09:43:55.367 に答える