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),又译为单一签入,一种对于许多相互关连,但是又是各自 ...
随机推荐
- Java 多线程学习扩展
http://www.imooc.com/video/5176 一.如何扩展Java并发知识 Java Memory Mode JMM描述了Java线程如何通过内存进行交互 happens-befor ...
- 《DSL》笔记一、什么是DSL(转)
1.1.问题域与解答域 1.1.1.什么是DSL? DSL(Domain-Specific Language)全称领域专用语言,就是专门用户特定领域的语言,看着概念觉得挺高大上的,其实很简单,就是专门 ...
- win10系统绑定本地IP和mac地址
第一步:找到自己的IP和mac地址 1.按着win键+R键,输入cmd(大小写都一样) 2.命令: ipconfig /all #查看所有地址 然后按“回车键” 3.这样 ...
- space-cloud 学习一 基本试用
space-cloud 是一个支持多数据库,以下是一个简单的基于官方文档的试用 使用docker-compose 运行 环境准备 下载docker-compose文件 wget https://raw ...
- NodeJS代码组织与部署
使用NodeJS编写程序前,为了有个良好的开端,首先需要准备好代码的目录结构和部署方式,就如同修房子要先搭脚手架.本章将介绍与之相关的各种知识. 一.模块路径解析规则 我们已经知道,require函数 ...
- 07-图5 Saving James Bond - Hard Version (30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- UmiJS可插拔的企业级 react 应用框架,配合ant-design-pro使用
入门非常简单 # 安装 $ yarn global add umi # 或者 npm install -g umi # 新建应用 $ mkdir myapp && cd myapp # ...
- 一种Winform类electron的实现
最近看了一篇文章 Winform客户端内嵌Vue页面 使用html作为winform的界面(其实这种做法早在MFC时代就已经有了),不过感觉文章中的封装并不够彻底,所以我忍不住要发一篇博客来说说我 ...
- Javascript Date构造函数和比较 (二)
JavaScript Date对象 构造函数实例 Date构造函数中没有参数,将返回当前日期 var currentDate = new Date(); writeLine(currentDate.t ...
- Ubuntu 14.04下超级终端Minicom连接ARM(转)
转自:https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70209765 笔者的工作环境: PC系统:Ubuntu 14.04 LT ...