Spring?Security安全框架之記住我功能
簡介
在一般的網(wǎng)站中,比如Bilibili。當(dāng)用戶登錄成功后,關(guān)閉瀏覽器后,下次重新進(jìn)入網(wǎng)站,可以自動登錄。
本次就來探究如何實現(xiàn)這種自動登錄、記住我的功能。
思維邏輯
需要實現(xiàn)記住我的功能操作,需要保證具體的實現(xiàn)方式,接下來就來梳理下。
首先,在Security框架中,針對記住我的功能實現(xiàn),Security框架本身對其做了一定的封裝,其實現(xiàn)原理為:
- 瀏覽器:cookie中存儲加密后的數(shù)據(jù)串
- 數(shù)據(jù)庫:MySQL等數(shù)據(jù)庫中存儲
加密后的數(shù)據(jù)串和用戶基本信息數(shù)據(jù)。
當(dāng)認(rèn)證器中,根據(jù)客戶端傳遞的cookie值,查詢服務(wù)器,符合用戶基本信息,則自動放行。
配置和測試
數(shù)據(jù)庫創(chuàng)建
本質(zhì)上是不要創(chuàng)建的,Security框架針對記住我功能的實現(xiàn),會在數(shù)據(jù)庫不存在表時,org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl自動創(chuàng)建一個persistent_logins表。
如果想手動創(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ù)庫對象
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ù)庫對象
* @return
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
jdbcTokenRepository.setCreateTableOnStartup(true); // 如果沒有配置記住我的數(shù)據(jù)表,則自動生成
return jdbcTokenRepository;
}配置config(HttpSecurity)
.and() .rememberMe().tokenRepository(persistentTokenRepository()) // 配置記住我的功能,同時增加查詢數(shù)據(jù)庫 cookie 值 .tokenValiditySeconds(60) // 設(shè)置cookie的有效時間(秒為單位) .userDetailsService(mySecurityService) // 查詢數(shù)據(jù)庫
頁面增加記住我選項
<!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="記住我" />自動登錄<br/>
<input type="submit" value="login" /><br/>
</form>
</body>
</html>
主要代碼為:
<input type="checkbox" name="remember-me" title="記住我" />自動登錄<br/>
【注意:】name="remember-me" 這是固定寫法,否則無法識別!
測試
重啟服務(wù)器,請求需要被認(rèn)證的url:
http://localhost/login.html


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

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

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

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

1、瀏覽器請求服務(wù)器時,會先經(jīng)過UsernamePasswordAuthenticationFilter進(jìn)行認(rèn)證操作
2、如果UsernamePasswordAuthenticationFilter認(rèn)證成功,會調(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信息,存儲于數(shù)據(jù)表中,并且也存儲在瀏覽器 cookie 中。
5、當(dāng)瀏覽器關(guān)閉,下次登錄時,會根據(jù)之前設(shè)定的remember-me信息,在org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter中對其驗證!

代碼下載
springboot-security-09-rememberMe
到此這篇關(guān)于Spring Security安全框架之記住我的文章就介紹到這了,更多相關(guān)Spring Security記住我內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot調(diào)用Poi-tl實現(xiàn)渲染數(shù)據(jù)并生成Word文檔
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何調(diào)用Poi-tl實現(xiàn)渲染數(shù)據(jù)并生成Word文檔,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2023-09-09
IDEA SpringBoot 項目配置Swagger2的詳細(xì)教程
這篇文章主要介紹了IDEA SpringBoot 項目配置Swagger2的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
JAVA實現(xiàn)按時間段查詢數(shù)據(jù)操作
這篇文章主要介紹了JAVA實現(xiàn)按時間段查詢數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring中PathMatcher路徑匹配器的實現(xiàn)
Spring框架中的PathMatcher是一個接口,本文主要介紹了Spring中PathMatcher路徑匹配器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
使用springcloud+oauth2攜帶token去請求其他服務(wù)
這篇文章主要介紹了使用springcloud+oauth2攜帶token去請求其他服務(wù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java高級之HashMap中的entrySet()方法使用
這篇文章主要介紹了Java高級之HashMap中的entrySet()方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
熟練掌握J(rèn)ava8新特性之Stream API的全面應(yīng)用
Stream是Java8的一大亮點,是對容器對象功能的增強,它專注于對容器對象進(jìn)行各種非常便利、高效的 聚合操作(aggregate operation)或者大批量數(shù)據(jù)操作。Stream API借助于同樣新出現(xiàn)的Lambda表達(dá)式,極大的提高編程效率和程序可讀性,感興趣的朋友快來看看吧2021-11-11

