java记录登陆时间_Spring security如何实现记录用户登录时间功能

本文介绍了如何使用Spring Security的AuthenticationSuccessHandler接口记录用户登录时间。通过自定义LoginSuccessHandler类,继承SavedRequestAwareAuthenticationSuccessHandler,并在登录成功后更新用户最后登录时间。在配置文件中指定自定义处理器,确保登录成功后执行相应操作。测试表明,只有通过账号密码登录才会更新登录时间。
摘要由CSDN通过智能技术生成

一、原理分析

spring security提供了一个接口 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 Authentication object which was created during

* the authentication process.

*/

void onAuthenticationSuccess(HttpServletRequest request,

HttpServletResponse response, Authentication authentication)

throws IOException, ServletException;

}

我们可以通过实现该接口来自定义登录成功后的操作,但spring security提供了一个SavedRequestAwareAuthenticationSuccessHandler实现类,这个实现类可以记住用户未登录前要访问的地址,这样登录成功后就可以把用户再跳转到他想去的页面。所以我们一般使用继承这个类的方式来实现自定义登录后续操作的功能。

二、实现方式

2.1 自定义Auth

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值