日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時間:2023/12/2 javascript 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java记录登陆时间_Spring security如何实现记录用户登录时间功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、原理分析

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;

}

我們可以通過實現(xiàn)該接口來自定義登錄成功后的操作,但spring security提供了一個SavedRequestAwareAuthenticationSuccessHandler實現(xiàn)類,這個實現(xiàn)類可以記住用戶未登錄前要訪問的地址,這樣登錄成功后就可以把用戶再跳轉(zhuǎn)到他想去的頁面。所以我們一般使用繼承這個類的方式來實現(xiàn)自定義登錄后續(xù)操作的功能。

二、實現(xiàn)方式

2.1 自定義AuthenticationSuccessHandler實現(xiàn)類

自定義AuthenticationSuccessHandler接口的實現(xiàn)類,繼承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);

//調(diào)用父類的方法把用戶引導到未登錄前要去的頁面

super.onAuthenticationSuccess(request,response,authentication);

}

}

其中remember-me-parameter="remembermeParamater"指定前臺傳遞的是否rememberme的參數(shù)名,前臺要傳遞的參數(shù)值是true或false

2.2 在spring-security的配置文件中指定自定義的AuthenticationSuccessHandler

username-parameter="username" password-parameter="password"

authentication-failure-forward-url="/failed.html"

default-target-url="/index.html"

authentication-success-handler-ref="loginSuccessHandler"

/>

實例上就是在定義自定義登錄頁面的標簽內(nèi)指定authentication-success-handler-ref="loginSuccessHandler",其中l(wèi)oginSuccessHandler是自定義的這個bean在容器中的名稱

2.3 測試

啟動工程,進行登錄,登錄成功后會更新用戶表中的last_login_time字段。

需要注意的是如果是通過readme進行的登錄,不會更新當前用戶的登錄時間,只有通過賬號密碼登錄時才會進行更新,也就是只有這時才會執(zhí)行這個onAuthenticationSuccess方法

三、總結(jié)

在用戶登錄成功后記錄本次登錄相關的信息,需要繼承spring-security提供的SavedRequestAwareAuthenticationSuccessHandler類,重寫其中的onAuthenticationSuccess方法,在其中進行記錄用戶信息的操作,在方法的最后調(diào)用父類的方法把用戶引導到未登錄前要去的頁面。

測試工程代碼的地址:工程示例

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的java记录登陆时间_Spring security如何实现记录用户登录时间功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。