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

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

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

簡介

在一般的網(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

gitee 代碼下載

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

相關(guān)文章

最新評論