spring - 使用Spring Security成功登录后如何正确更新登录日期时间?

标签 spring spring-mvc spring-security

我正在使用Spring 3.2.0和相同版本的Spring安全性。成功登录后,将用户重定向到 protected 页面之一,如下所示。

public final class LoginSuccessHandler implements AuthenticationSuccessHandler
{
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
    {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN"))
        {
            response.sendRedirect("admin_side/Home.htm");
            return;
        }
    }
}

我正在使用Hibernate。成功登录后,如何更新数据库中的登录日期时间(上次登录)?我在登录页面上有一个提交按钮,其POST请求似乎未映射到其相应的登录 Controller 中的方法。实际上,登录表单的操作已映射到Servlet-j_spring_security_check

如果需要,整个spring-security.xml文件如下。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http pattern="/Login.htm*" security="none"></http>    

    <http auto-config='true'>
    <!--<remember-me key="myAppKey"/>-->
        <session-management session-fixation-protection="newSession">
            <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
        </session-management>

        <intercept-url pattern="/admin_side/**" access="ROLE_ADMIN" requires-channel="any"/>
        <form-login login-page="/" default-target-url="/admin_side/Home.htm" authentication-failure-url="/LoginFailed.htm" authentication-success-handler-ref="loginSuccessHandler"/>
        <logout logout-success-url="/Login.htm" invalidate-session="true" delete-cookies="JSESSIONID"/>
    </http>

    <authentication-manager>
       <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
               users-by-username-query="select email_id, password, enabled from user_table where lower(email_id)=lower(?)"
               authorities-by-username-query="select ut.email_id, ur.authority from user_table ut, user_roles ur where ut.user_id=ur.user_id and lower(ut.email_id)=lower(?)"/>
       </authentication-provider>
    </authentication-manager>

    <beans:bean id="loginSuccessHandler" class="loginsuccesshandler.LoginSuccessHandler"/>

    <global-method-security>
        <protect-pointcut expression="execution(* dao.*.*(..))" access="ROLE_ADMIN"/>
    </global-method-security>

    <!--<global-method-security secured-annotations="enabled" />-->
</beans:beans>

最佳答案

另一种方法是为 AuthenticationSuccessEvent 注册一个处理程序。

    @Service
    public class UserService implements
                             ApplicationListener<AuthenticationSuccessEvent> {

        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent event) {
           String userName = ((UserDetails) event.getAuthentication().
                                                  getPrincipal()).getUsername();
           User user = this.userDao.findByLogin(userName);
           user.setLastLoginDate(new Date());
        }
   }

关于spring - 使用Spring Security成功登录后如何正确更新登录日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493237/

相关文章:

java - 不兼容的类型 : java. util.Optional<xyz.app.entity.Users> 无法转换为Users(Springboot编译错误)

java - 如何在没有 web.xml 的情况下在 DispatcherServlet 上下文中捕获 Spring PageNotFound 异常?

java - Spring Resttemplate登录失败

java.sql.SQLException : Connection is null 异常

security - JSON Web token 过期

java - 根据数据库中的数据检查对象的内存版本

spring - 如何向 Spring Security userdetails 添加其他详细信息

java - CSRF 验证/验证不适用于导出的 war 版本 - 使用 OWASP CSRFGuard 时

java - org.springframework.web.client.HttpClientErrorException : 415 null(Spring Resttemplate)

Java Spring application.properties 环境变量或默认值