如何使用Spring Security 4记录成功登录



我有一个使用Spring Security 4的Spring MVC 4应用程序。我正在尝试在我的系统中记录每一次成功登录。我已经尝试过AuthenticationSuccessEvent解决方案,但没有成功。

我正在使用Spring MVC 4和Spring Security 4

我的登录表单代码是:

<c:url value="/login" var="loginUrl" />
            <form action="${loginUrl}" method='POST'>
                <fieldset>
                    <legend>Login</legend>
                    <table>
                        <tr>
                            <td><label for="username">Username</label></td>
                            <td><input type="text" id="username" name="username" /></td>
                        </tr>
                        <tr>
                            <td><label for="password">Password</label></td>
                            <td><input type="password" id="password" name="password" /></td>
                        </tr>
                        <tr>
                        <th colspan="2"><input id="send" type="submit" value="Login"></th>
                    </table>
                    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                    <c:if test="${param.error != null}">
                        <div class="loginError" style="text-align: center">Invalid username and/or password. Please try again.</div>
                    </c:if>
                </fieldset>
            </form>

有什么建议吗?

感谢

经过几次尝试,我终于拿到了。

首先创建CustomLoginHandler

public class CustomLoginSucessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication authentication)
            throws ServletException, IOException {
        User user = (User) authentication.getPrincipal();
            // record login success of user
        super.onAuthenticationSuccess(request, response, authentication);
    }

稍后声明如果在您的Configuration Bean

就我而言,我这样做了:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().successHandler(new CustomLoginSucessHandler());
}

现在,当我登录时,我可以在我的CustomLoginHandler 中做任何我想做的事情

最新更新