Spring?Security安全框架之記住我功能
簡介
在一般的網(wǎng)站中,比如Bilibili
。當用戶登錄成功后,關(guā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ù)。
當認證器中,根據(jù)客戶端傳遞的cookie值,查詢服務器,符合用戶基本信息,則自動放行。
配置和測試
數(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進行表的創(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) // 設置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" 這是固定寫法,否則無法識別!
測試
重啟服務器,請求需要被認證的url:
http://localhost/login.html
關(guān)閉瀏覽器,重新打開,重新請求:
http://localhost/loginSuccess.html
注意事項
由于數(shù)據(jù)庫
本身未創(chuàng)建
persistent_logins
表,只是在配置類中申明創(chuàng)建表
:
此時數(shù)據(jù)庫中,可以看到已存在表信息:
此處需要注意的是,如果配置了自動創(chuàng)建表,如果已存在指定的表,啟動會報錯!
原理分析
1、瀏覽器請求服務器時,會先經(jīng)過UsernamePasswordAuthenticationFilter
進行認證操作
2、如果UsernamePasswordAuthenticationFilter
認證成功,會調(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、當瀏覽器關(guān)閉,下次登錄時,會根據(jù)之前設定的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)文章
解決BeanUtils.copyProperties無法成功封裝的問題
這篇文章主要介紹了解決BeanUtils.copyProperties無法成功封裝的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java數(shù)據(jù)結(jié)構(gòu)之LinkedList的用法詳解
鏈表(Linked?list)是一種常見的基礎數(shù)據(jù)結(jié)構(gòu),是一種線性表。Java的LinkedList(鏈表)?類似于?ArrayList,是一種常用的數(shù)據(jù)容器,本文就來簡單講講它的使用吧2023-05-05Java中 this和super的用法與區(qū)別小結(jié)
在Java的學習與開發(fā)者我們經(jīng)常遇到this和super關(guān)鍵字,本文主要介紹了Java中 this和super的用法與區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-12-12java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01SpringBoot2使用JTA組件實現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務管理(親測好用)
這篇文章主要介紹了SpringBoot2使用JTA組件實現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務管理(親測好用),在Spring?Boot?2.x中,整合了這兩個JTA的實現(xiàn)分別是Atomikos和Bitronix,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07