Spring?Security安全框架之記住我功能
簡(jiǎn)介
在一般的網(wǎng)站中,比如Bilibili。當(dāng)用戶登錄成功后,關(guān)閉瀏覽器后,下次重新進(jìn)入網(wǎng)站,可以自動(dòng)登錄。
本次就來(lái)探究如何實(shí)現(xiàn)這種自動(dòng)登錄、記住我的功能。
思維邏輯
需要實(shí)現(xiàn)記住我的功能操作,需要保證具體的實(shí)現(xiàn)方式,接下來(lái)就來(lái)梳理下。
首先,在Security框架中,針對(duì)記住我的功能實(shí)現(xiàn),Security框架本身對(duì)其做了一定的封裝,其實(shí)現(xiàn)原理為:
- 瀏覽器:cookie中存儲(chǔ)加密后的數(shù)據(jù)串
- 數(shù)據(jù)庫(kù):MySQL等數(shù)據(jù)庫(kù)中存儲(chǔ)
加密后的數(shù)據(jù)串和用戶基本信息數(shù)據(jù)。
當(dāng)認(rèn)證器中,根據(jù)客戶端傳遞的cookie值,查詢服務(wù)器,符合用戶基本信息,則自動(dòng)放行。
配置和測(cè)試
數(shù)據(jù)庫(kù)創(chuàng)建
本質(zhì)上是不要?jiǎng)?chuàng)建的,Security框架針對(duì)記住我功能的實(shí)現(xiàn),會(huì)在數(shù)據(jù)庫(kù)不存在表時(shí),org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl自動(dòng)創(chuàng)建一個(gè)persistent_logins表。
如果想手動(dòng)創(chuàng)建,可以執(zhí)行下列sql進(jìn)行表的創(chuàng)建:
CREATE TABLE persistent_logins ( username VARCHAR ( 64 ) NOT NULL, series VARCHAR ( 64 ) PRIMARY KEY, token VARCHAR ( 64 ) NOT NULL, last_used TIMESTAMP NOT NULL );
配置類注入數(shù)據(jù)源,配置操作數(shù)據(jù)庫(kù)對(duì)象
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import javax.sql.DataSource;
/**
* 注入數(shù)據(jù)源(記住我)
*/
@Autowired
private DataSource dataSource;
/**
* 配置操作數(shù)據(jù)庫(kù)對(duì)象
* @return
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
jdbcTokenRepository.setCreateTableOnStartup(true); // 如果沒(méi)有配置記住我的數(shù)據(jù)表,則自動(dòng)生成
return jdbcTokenRepository;
}配置config(HttpSecurity)
.and() .rememberMe().tokenRepository(persistentTokenRepository()) // 配置記住我的功能,同時(shí)增加查詢數(shù)據(jù)庫(kù) cookie 值 .tokenValiditySeconds(60) // 設(shè)置cookie的有效時(shí)間(秒為單位) .userDetailsService(mySecurityService) // 查詢數(shù)據(jù)庫(kù)
頁(yè)面增加記住我選項(xiàng)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
用戶名:<input type="text" name="username" /><br/>
密碼:<input type="text" name="password" /><br/>
<input type="checkbox" name="remember-me" title="記住我" />自動(dòng)登錄<br/>
<input type="submit" value="login" /><br/>
</form>
</body>
</html>
主要代碼為:
<input type="checkbox" name="remember-me" title="記住我" />自動(dòng)登錄<br/>
【注意:】name="remember-me" 這是固定寫(xiě)法,否則無(wú)法識(shí)別!
測(cè)試
重啟服務(wù)器,請(qǐng)求需要被認(rèn)證的url:
http://localhost/login.html


關(guān)閉瀏覽器,重新打開(kāi),重新請(qǐng)求:
http://localhost/loginSuccess.html

注意事項(xiàng)
由于數(shù)據(jù)庫(kù)本身未創(chuàng)建 persistent_logins表,只是在配置類中申明創(chuàng)建表:

此時(shí)數(shù)據(jù)庫(kù)中,可以看到已存在表信息:

此處需要注意的是,如果配置了自動(dòng)創(chuàng)建表,如果已存在指定的表,啟動(dòng)會(huì)報(bào)錯(cuò)!
原理分析

1、瀏覽器請(qǐng)求服務(wù)器時(shí),會(huì)先經(jīng)過(guò)UsernamePasswordAuthenticationFilter進(jìn)行認(rèn)證操作
2、如果UsernamePasswordAuthenticationFilter認(rèn)證成功,會(huì)調(diào)用其父類 AbstractAuthenticationProcessingFilter中的doFilter方法。
3、最終執(zhí)行this.successfulAuthentication(request, response, chain, authResult)。

即org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices中的loginSuccess方法。


然后在org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#onLoginSuccess中做以下操作:

4、將生成的新的token信息,存儲(chǔ)于數(shù)據(jù)表中,并且也存儲(chǔ)在瀏覽器 cookie 中。
5、當(dāng)瀏覽器關(guān)閉,下次登錄時(shí),會(huì)根據(jù)之前設(shè)定的remember-me信息,在org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter中對(duì)其驗(yàn)證!

代碼下載
springboot-security-09-rememberMe
到此這篇關(guān)于Spring Security安全框架之記住我的文章就介紹到這了,更多相關(guān)Spring Security記住我內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Security 實(shí)現(xiàn)“記住我”功能及原理解析
- Spring security實(shí)現(xiàn)記住我下次自動(dòng)登錄功能過(guò)程詳解
- Spring Security實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄"記住我"功能
- Spring Security 構(gòu)建rest服務(wù)實(shí)現(xiàn)rememberme 記住我功能
- SpringBoot整合Security安全框架實(shí)現(xiàn)控制權(quán)限
- Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式
相關(guān)文章
java根據(jù)List內(nèi)對(duì)象的屬性排序方法
下面小編就為大家分享一篇java根據(jù)List內(nèi)對(duì)象的屬性排序方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題
這篇文章主要介紹了解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java數(shù)據(jù)結(jié)構(gòu)之LinkedList的用法詳解
鏈表(Linked?list)是一種常見(jiàn)的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表。Java的LinkedList(鏈表)?類似于?ArrayList,是一種常用的數(shù)據(jù)容器,本文就來(lái)簡(jiǎn)單講講它的使用吧2023-05-05
SpringBoot實(shí)戰(zhàn)之SSL配置詳解
今天小編就為大家分享一篇關(guān)于SpringBoot實(shí)戰(zhàn)之SSL配置詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
Java中 this和super的用法與區(qū)別小結(jié)
在Java的學(xué)習(xí)與開(kāi)發(fā)者我們經(jīng)常遇到this和super關(guān)鍵字,本文主要介紹了Java中 this和super的用法與區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題
這篇文章主要介紹了java 基礎(chǔ)之JavaBean屬性命名規(guī)范問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-05-05
java判斷l(xiāng)ist不為空的實(shí)現(xiàn),和限制條數(shù)不要在一起寫(xiě)
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實(shí)現(xiàn),和限制條數(shù)不要在一起寫(xiě)。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測(cè)好用)
這篇文章主要介紹了SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測(cè)好用),在Spring?Boot?2.x中,整合了這兩個(gè)JTA的實(shí)現(xiàn)分別是Atomikos和Bitronix,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

