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实现记录用户登录时间等信息的更多相关文章

  1. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  2. PPTP-VPN日志功能,记录用户登录时间,流量统计,IP地址等信息

    我们先看两个文件 /etc/ppp/ip-up /etc/ppp/ip-down 这两个文件为shell脚本,当PPTP用户连接或者断开时分别执行这两个文件,并且会带相应的参数 这些参数有 $PEER ...

  3. pptpvpn记录用户登录和流量信息

    这个问题困扰了我很久,终于在pppd的man文档里,发现了踪迹.在man中的SCRIPTS下有一系列的参数,其中PEERNAME就是登陆的用户名,并且在/etc/ppp/ip-up和/etc/ppp/ ...

  4. Spring Security 入门(1-3-1)Spring Security - http元素 - 默认登录和登录定制

    登录表单配置 - http 元素下的 form-login 元素是用来定义表单登录信息的.当我们什么属性都不指定的时候 Spring Security 会为我们生成一个默认的登录页面. 如果不想使用默 ...

  5. spring security 动态 修改当前登录用户的 权限

    1.前言 spring security 可以获取当前登录的用户信息,同时提供了接口 来修改权限列表信息 , 使用这个方法 ,可以动态的修改当前登录用户权限. 那么问题来了... 如果我是管理员 ,如 ...

  6. Spring Security之多次登录失败后账户锁定功能的实现

    在上一次写的文章中,为大家说到了如何动态的从数据库加载用户.角色.权限信息,从而实现登录验证及授权.在实际的开发过程中,我们通常会有这样的一个需求:当用户多次登录失败的时候,我们应该将账户锁定,等待一 ...

  7. Spring Security调研记录【七】--核心模型与实现

    网上有非常多关于Spring Security文章中,都觉得Spring Security(相对于shiro)过于复杂,个人觉得复杂的是Spring Security的官方文档而不是Spring Se ...

  8. Spring security 获取当前用户

    spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签   1 <%@ taglib prefix=" ...

  9. Spring Security OAuth2 SSO 单点登录

    基于 Spring Security OAuth2 SSO 单点登录系统 SSO简介 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自 ...

随机推荐

  1. UI系统综述:iOS的图形绘制、动画与runloop

    一.一条业务pipeline: 一个连接核心:coreanimation 二.两个进程: 1.app进程: 2.render进程: 首先,由 app 处理事件(Handle Events),如:用户的 ...

  2. 函数式编程:面向可复用的map和pipeline机制的编程语言

    函数式编程:面向可复用的map和pipeline机制的编程语言

  3. Content-Type与MIME

    http://www.cnblogs.com/jsean/articles/1610265.html 首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF. ...

  4. 异步模型:上下文与时间---async = request + reponse + handler + context + time;

    futureHandler = current(handler, context(t0)) : T0    ->    handler(context(t0),taskResult) : Tx ...

  5. SC CSP-J2019初赛成绩已出!

    链接: https://pan.baidu.com/s/1UK2pL7UW0n0vYpnzMbJm9A 提取码: uwav 复制这段内容后打开百度网盘手机App,操作更方便哦 Me?87! I am  ...

  6. java 调度框架quartz

    核心代码如下: public class SchedulerTest { public static void main(String[] args) { //创建schedulerFactory类 ...

  7. MySQL 8.0 以上版本重置 root 用户密码的终极办法

    1. 在 /etc/my.cnf 文件末尾追加 skip-grant-tables [root@abdefg mysql]# vim /etc/my.cnf [mysql] # 设置mysql客户端默 ...

  8. itext7 html转pdf实现

    公司最近做一个交易所项目,里面涉及一个需求就是将html模板,在填充数据后转换为pdf,这样防止数据更改,下面是具体实现 1 pom文件 <dependency> <groupId& ...

  9. Android Studio 屏幕方向以及UI界面状态的保存

    package com.example.orientation; import android.os.Bundle; import android.util.Log; import android.v ...

  10. 范仁义html+css课程---10、其它标签

    范仁义html+css课程---10.其它标签 一.总结 一句话总结: 了解iframe.Figure与Figcaption.address.progress.meter.datalist.field ...