欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Security安全框架之記住我功能

 更新時(shí)間:2022年02月24日 14:48:17   作者:專(zhuān)注寫(xiě)bug  
這篇文章主要介紹了Spring?Security安全框架之記住我,本次就來(lái)探究如何實(shí)現(xiàn)這種自動(dòng)登錄、記住我的功能,通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下

簡(jiǎn)介

在一般的網(wǎng)站中,比如Bilibili。當(dāng)用戶(hù)登錄成功后,關(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ù)串用戶(hù)基本信息數(shù)據(jù)。

當(dāng)認(rèn)證器中,根據(jù)客戶(hù)端傳遞的cookie值,查詢(xún)服務(wù)器,符合用戶(hù)基本信息,則自動(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 
);

配置類(lèi)注入數(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í)增加查詢(xún)數(shù)據(jù)庫(kù) cookie 值
.tokenValiditySeconds(60)  // 設(shè)置cookie的有效時(shí)間(秒為單位)
.userDetailsService(mySecurityService) // 查詢(xún)數(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">
        用戶(hù)名:<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表,只是在配置類(lèi)中申明創(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)用其父類(lèi) 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

gitee 代碼下載

到此這篇關(guān)于Spring Security安全框架之記住我的文章就介紹到這了,更多相關(guān)Spring Security記住我內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論