spring security实现记录用户登录时间等信息
spring security实现记录用户登录时间等信息
上一篇: spring security实现记住我下次自动登录功能
一、原理分析
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 <tt>Authentication</tt> object which was created during
* the authentication process.
*/
void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException;
}
我们可以通过实现该接口来自定义登录成功后的操作,但spring security提供了一个SavedRequestAwareAuthenticationSuccessHandler实现类,这个实现类可以记住用户未登录前要访问的地址,这样登录成功后就可以把用户再跳转到他想去的页面。所以我们一般使用继承这个类的方式来实现自定义登录后续操作的功能。
二、实现方式
2.1 自定义AuthenticationSuccessHandler实现类
自定义AuthenticationSuccessHandler接口的实现类,继承SavedRequestAwareAuthenticationSuccessHandler类,并加入到spring容器中
@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"指定前台传递的是否rememberme的参数名,前台要传递的参数值是true或false
2.2 在spring-security的配置文件中指定自定义的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是自定义的这个bean在容器中的名称
2.3 测试
启动工程,进行登录,登录成功后会更新用户表中的last_login_time 字段。

需要注意的是如果是通过readme进行的登录,不会更新当前用户的登录时间,只有通过账号密码登录时才会进行更新,也就是只有这时才会执行这个onAuthenticationSuccess方法
三、总结
在用户登录成功后记录本次登录相关的信息,需要继承spring-security提供的SavedRequestAwareAuthenticationSuccessHandler类,重写其中的onAuthenticationSuccess方法,在其中进行记录用户信息的操作,在方法的最后调用父类的方法把用户引导到未登录前要去的页面。
测试工程代码的地址:工程示例
spring security实现记录用户登录时间等信息的更多相关文章
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- PPTP-VPN日志功能,记录用户登录时间,流量统计,IP地址等信息
我们先看两个文件 /etc/ppp/ip-up /etc/ppp/ip-down 这两个文件为shell脚本,当PPTP用户连接或者断开时分别执行这两个文件,并且会带相应的参数 这些参数有 $PEER ...
- pptpvpn记录用户登录和流量信息
这个问题困扰了我很久,终于在pppd的man文档里,发现了踪迹.在man中的SCRIPTS下有一系列的参数,其中PEERNAME就是登陆的用户名,并且在/etc/ppp/ip-up和/etc/ppp/ ...
- Spring Security 入门(1-3-1)Spring Security - http元素 - 默认登录和登录定制
登录表单配置 - http 元素下的 form-login 元素是用来定义表单登录信息的.当我们什么属性都不指定的时候 Spring Security 会为我们生成一个默认的登录页面. 如果不想使用默 ...
- spring security 动态 修改当前登录用户的 权限
1.前言 spring security 可以获取当前登录的用户信息,同时提供了接口 来修改权限列表信息 , 使用这个方法 ,可以动态的修改当前登录用户权限. 那么问题来了... 如果我是管理员 ,如 ...
- Spring Security之多次登录失败后账户锁定功能的实现
在上一次写的文章中,为大家说到了如何动态的从数据库加载用户.角色.权限信息,从而实现登录验证及授权.在实际的开发过程中,我们通常会有这样的一个需求:当用户多次登录失败的时候,我们应该将账户锁定,等待一 ...
- Spring Security调研记录【七】--核心模型与实现
网上有非常多关于Spring Security文章中,都觉得Spring Security(相对于shiro)过于复杂,个人觉得复杂的是Spring Security的官方文档而不是Spring Se ...
- Spring security 获取当前用户
spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签 1 <%@ taglib prefix=" ...
- Spring Security OAuth2 SSO 单点登录
基于 Spring Security OAuth2 SSO 单点登录系统 SSO简介 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自 ...
随机推荐
- ReadIniTest_GetPrivateProfileString
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- reactnative 笔记
1.<FlatList/> _renderItem = ({item})=>{ return <View style={[styles.part4Row]}> <T ...
- CENTOS 7 下安装 REDIS 5.0.6 完整步骤
第一步:下载redis安装包 wget http://download.redis.io/releases/redis-5.0.6.tar.gz 第二步:解压压缩包 tar -zxvf redis ...
- 60: noi.ac #69
$code$ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g ...
- csp 201903-3 损坏的RAID5
问题描述 试题编号: 201903-3 试题名称: 损坏的RAID5 时间限制: 1.0s 内存限制: 512.0MB 问题描述: 答题栏 核
- SQL进阶-索引设置&sql优化
一.索引设置 1.索引的设置原则 经常出现在WHERE条件.关联条件中的字段作为索引字段: 在满足查询需求的前提下,应尽可能少的创建索引:(对于一个组合索引,可以满足以组合索引左边的一部分字段的查询需 ...
- 使用GParted调整ubuntu根目录空间大小
一.背景 Win10系统下安装ubuntu16.04双系统-常见问题解答 由于安装双系统时,ubuntu分区设置如下: 1) 主分区 ext4 / 30720MB 2) 主分区 swap area 8 ...
- NSGA-II算法学习
什么是支配: 支配就是统治,在各方面都优于其余个体 如个体i支配个体j,就说明个体i在所有目标函数的表现上都不差于个体j,并且至少在一个目标上优于个体j: 什么是非支配: 非支配就是个体在种群中是最优 ...
- Python开发:OpenCV版本差异所引发的cv2.findContours()函数传参问题
一.问题如下: cv2.findContours()这个方法是用来找出轮廓值的: # cv2.findContours找出轮廓值,cv2.RETR_EXTERNAL表示图像的外轮廓 binary, c ...
- 【Beta阶段】第八次Scrum Meeting
每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #63 技术博客--django和mysqlhttps://github.com/rRetr0Git/rateMyCourse/issues ...