久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Spring security如何實現記錄用戶登錄時間功能

瀏覽:73日期:2023-09-17 15:32:25

一、原理分析

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字段。

Spring security如何實現記錄用戶登錄時間功能

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

三、總結

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 91在线观看视频 | 成人在线观看免费视频 | 欧美日韩在线精品 | 噜噜噜在线观看免费视频日本 | 欧美久久久久久久 | 国产自产才c区 | 中文字幕一区二区三区四区五区 | 日本欧美久久久久 | 久久精品一区二区三区四区 | 成人在线观看免费视频 | 最新高清无码专区 | 一级毛片视屏 | 国产成人av免费 | 亚洲精品女人久久 | 成人在线观看网站 | 久久久.com| 成视频年人免费看黄网站 | 丁香五月亚洲综合在线 | 欧美日韩不卡在线 | 男女视频在线免费观看 | 一区二区三区回区在观看免费视频 | 成人亚洲 | 中文字幕一区二区三区不卡 | 欧美日韩一区二区三区在线观看 | 国产精品69久久久久水密桃 | 免费久久久 | 亚洲精品久久久久久一区二区 | www.亚洲成人网 | 国产激情视频在线观看 | 国产激情偷乱视频一区二区三区 | 亚洲视频一| 日本二区在线播放 | av综合在线观看 | 噜噜噜噜狠狠狠7777视频 | 一区二区三区小视频 | av片网| 一区二区三区精品视频 | 91高清在线| 亚洲成av人片在线观看无码 | 欧美国产精品久久久 | 久久草视频|