春のセキュリティは、ユーザのログイン時間などの情報を記録を達成しました

春のセキュリティは、ユーザのログイン時間などの情報を記録を達成しました

前:春のセキュリティは、自動ログオンを達成するために私の次の時間を覚えています

まず、原則として分析

春のセキュリティはインタフェースAuthenticationSuccessHandlerを提供し、このインタフェースは、ログイン成功後の動作を実行するための唯一の方法であり、


public interface AuthenticationSuccessHandler {

    /**
     * Called when a user has been successfully authenticated.
     *
     * @param request the request which caused the successful authentication
     * @param response the response
     * @param authentication the <tt>Authentication</tt> object which was created during
     * the authentication process.
     */
    void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException;

}

私たちは、インターフェイスをカスタマイズするためにログイン成功後の動作を実現することができますが、春のセキュリティが提供するSavedRequestAwareAuthenticationSuccessHandler実装クラスを、実装クラスは、ユーザーがログインしていない前に、アクセスするアドレスを覚えることができるので、ログイン成功後、ユーザーはにジャンプすることができます彼はページを望んでいました。だから我々は、一般的に、カスタム関数のその後の動作に看板のこのタイプを実装するために、継承を使用しています。

第二に、実装

2.1カスタム実装クラスAuthenticationSuccessHandler

AuthenticationSuccessHandlerカスタムインタフェースクラスの実装、SavedRequestAwareAuthenticationSuccessHandler継承するクラス、スプリング及び容器に添加

@Component("loginSuccessHandler")
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private IUserDao userDao;

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //记录相关的用户信息,如上次登录时间
        String name = authentication.getName();
        userDao.updateLastLonginTime(System.currentTimeMillis(),name);

        //调用父类的方法把用户引导到未登录前要去的页面
        super.onAuthenticationSuccess(request,response,authentication);
    }
}

どのremember-me-parameter="remembermeParamater"転送フロントデスクリメンバー・ミーパラメータ名、パラメータ値が渡される前景色が真か偽であるかどうかを指定します

2.2カスタムスプリング - セキュリティプロファイルに指定されAuthenticationSuccessHandler

<!--自定义登录页面-->
        <security:form-login login-page="/login.html" login-processing-url="/login"
                             username-parameter="username" password-parameter="password"
                             authentication-failure-forward-url="/failed.html"
                             default-target-url="/index.html"
                             authentication-success-handler-ref="loginSuccessHandler"

        />

例に指定するには、カスタムログインページを定義するタグであるauthentication-success-handler-ref="loginSuccessHandler"容器に豆のどのloginSuccessHandlerカスタム名を

2.3テスト

スタートプロジェクトは、ユーザーがログインに成功の後にテーブルを更新することができ、ログインlast_login_timeフィールド。

ログインはREADMEている場合、時間は現在ログオンしているユーザーにのみ、このメソッドの実装は意志、である、唯一のログインアカウントのパスワードによって更新されますが更新されないことに注意してくださいonAuthenticationSuccess

第三に、要約

このログインに関連した成功した情報記録ユーザーがログインした後、あなたは春・セキュリティの継承を提供する必要がありSavedRequestAwareAuthenticationSuccessHandler、最後の方法で操作記録のユーザ情報は、ユーザーガイドの方法の父と呼ばれているクラス、これらのメソッドのオーバーライドonAuthenticationSuccessを、行く前のページにログオンしていません。

アドレステストプロジェクトコード:エンジニアリング例

おすすめ

転載: www.cnblogs.com/chengxuxiaoyuan/p/11967541.html
おすすめ