亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能

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

一、原理分析

spring security提供了一個(gè)接口 AuthenticationSuccessHandler,該接口中只有一個(gè)方法,用來進(jìn)行登錄成功后的操作

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;}

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

二、實(shí)現(xiàn)方式

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

自定義AuthenticationSuccessHandler接口的實(shí)現(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 { //記錄相關(guān)的用戶信息,如上次登錄時(shí)間 String name = authentication.getName(); userDao.updateLastLonginTime(System.currentTimeMillis(),name); //調(diào)用父類的方法把用戶引導(dǎo)到未登錄前要去的頁面 super.onAuthenticationSuccess(request,response,authentication); }}

其中remember-me-parameter='remembermeParamater'指定前臺(tái)傳遞的是否rememberme的參數(shù)名,前臺(tái)要傳遞的參數(shù)值是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' />

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

2.3 測試

啟動(dòng)工程,進(jìn)行登錄,登錄成功后會(huì)更新用戶表中的last_login_time字段。

Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能

需要注意的是如果是通過readme進(jìn)行的登錄,不會(huì)更新當(dāng)前用戶的登錄時(shí)間,只有通過賬號(hào)密碼登錄時(shí)才會(huì)進(jìn)行更新,也就是只有這時(shí)才會(huì)執(zhí)行這個(gè)onAuthenticationSuccess方法

三、總結(jié)

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 东北普通话清晰对白 | 一级特黄特黄的大片免费 | 国产成人精品一区二区免费视频 | 欧美日韩在线免费 | 久久在线免费 | 黄色激情视频网站 | 免费国产不卡午夜福在线观看 | a级毛片黄 | 高清精品美女在线播放 | 1000部禁片黄的免费看 | 国产一区二区三区四区 | 午夜黄页网站在线播放 | 日本在线日本中文字幕日本在线视频播放 | 免费观看成人羞羞视频网站观看 | 最新亚洲手机在线人成网站 | 99视频国产在线 | 999精品 | mmmmxxxx国产在线观看 | 欧美日韩1区2区 | 国产精品成aⅴ人片在线观看 | 成人三级图片 | 精品日韩欧美一区二区三区 | 日本黄色一级网站 | 痴情的后裔韩剧免费观看中文高清版 | 91精品国产肉丝高跟在线 | 天天看天天射天天碰 | 一级国产精品一级国产精品片 | 一级特黄aa大片免费播放视频 | 黄色的网站免费观看 | 大片免费观看在线视频 | 欧美超长黑吊 | 精品国产亚洲一区二区三区 | 黄色影院在线观看 | 成人区视频爽爽爽爽爽 | 成人毛片网| 久久国产视频网站 | 看黄视频在线观看 | 国产在线视频在线观看 | 欧美高清在线视频一区二区 | 国产精品对白刺激久久久 | 精品视频入口 |