一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Spring Security 構建rest服務實現rememberme 記住我功能

Spring Security 構建rest服務實現rememberme 記住我功能

2021-04-09 11:40奮斗的羊仔 JAVA教程

這篇文章主要介紹了Spring Security 構建rest服務實現rememberme 記住我功能,需要的朋友可以參考下

spring security記住我基本原理:

登錄的時候,請求發送給過濾器usernamepasswordauthenticationfilter,當該過濾器認證成功后,會調用remembermeservice,會生成一個token,將token寫入到瀏覽器cookie,同時remembermeservice里邊還有個tokenrepository,將token和用戶信息寫入到數據庫中。這樣當用戶再次訪問系統,訪問某一個接口時,會經過一個remembermeauthenticationfilter的過濾器,他會讀取cookie中的token,交給rememberservice,rememberservice會用tokenrepository根據token從數據庫中查是否有記錄,如果有記錄會把用戶名取出來,再調用userdetailservice根據用戶名獲取用戶信息,然后放在securitycontext里。

Spring Security 構建rest服務實現rememberme 記住我功能

 remembermeauthenticationfilter在spring security中認證過濾器鏈的倒數第二個過濾器位置,當其他認證過濾器都沒法認證成功的時候,就會調用remembermeauthenticationfilter嘗試認證。

Spring Security 構建rest服務實現rememberme 記住我功能

實現:

 1,登錄表單加上<input type="checkbox" name="remember-me" value="true"/>,springsecurity在springsessionremembermeservices類里定義了一個常量,默認值就是remember-me

 2,根據上邊的原理圖可知,要配置tokenrepository,把生成的token存進數據庫,這是一個配置bean的配置,放在了browsersecurityconfig里

3,在configure里配置

4,在browserproperties里加上自動登錄時間,把記住我時間做成可配置的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//記住我秒數配置
private int remembermeseconds = 10;齊活
package com.imooc.s@configuration //這是一個配置
public class browsersecurityconfig extends websecurityconfigureradapter{
  //讀取用戶配置的登錄頁配置
  @autowired
  private securityproperties securityproperties;
  //自定義的登錄成功后的處理器
  @autowired
  private authenticationsuccesshandler imoocauthenticationsuccesshandler;
  //自定義的認證失敗后的處理器
  @autowired
  private authenticationfailurehandler imoocauthenticationfailurehandler;
  //數據源
  @autowired
  private datasource datasource;
  @autowired
  private userdetailsservice userdetailsservice;
  //注意是org.springframework.security.crypto.password.passwordencoder
  @bean
  public passwordencoder passwordencoder(){
    //bcryptpasswordencoder implements passwordencoder
    return new bcryptpasswordencoder();
  }
  /**
   * 記住我tokenrepository配置,在登錄成功后執行
   * 登錄成功后往數據庫存token的
   * @description: 記住我tokenrepository配置
   * @param @return  jdbctokenrepositoryimpl
   * @return persistenttokenrepository
   * @throws
   * @author lihaoyang
   * @date 2018年3月5日
   */
  @bean
  public persistenttokenrepository persistenttokenrepository(){
    jdbctokenrepositoryimpl jdbctokenrepository = new jdbctokenrepositoryimpl();
    jdbctokenrepository.setdatasource(datasource);
    //啟動時自動生成相應表,可以在jdbctokenrepositoryimpl里自己執行create_table_sql腳本生成表
    jdbctokenrepository.setcreatetableonstartup(true);
    return jdbctokenrepository;
  }
  //版本二:可配置的登錄頁
  @override
  protected void configure(httpsecurity http) throws exception {
    //驗證碼過濾器
    validatecodefilter validatecodefilter = new validatecodefilter();
    //驗證碼過濾器中使用自己的錯誤處理
    validatecodefilter.setauthenticationfailurehandler(imoocauthenticationfailurehandler);
    //配置的驗證碼過濾url
    validatecodefilter.setsecurityproperties(securityproperties);
    validatecodefilter.afterpropertiesset();
    //實現需要認證的接口跳轉表單登錄,安全=認證+授權
    //http.httpbasic() //這個就是默認的彈框認證
    //
    http //把驗證碼過濾器加載登錄過濾器前邊
      .addfilterbefore(validatecodefilter, usernamepasswordauthenticationfilter.class)
      //表單認證相關配置
      .formlogin()
        .loginpage("/authentication/require") //處理用戶認證browsersecuritycontroller
        //登錄過濾器usernamepasswordauthenticationfilter默認登錄的url是"/login",在這能改
        .loginprocessingurl("/authentication/form")
        .successhandler(imoocauthenticationsuccesshandler)//自定義的認證后處理器
        .failurehandler(imoocauthenticationfailurehandler) //登錄失敗后的處理
      .and()
      //記住我相關配置 
      .rememberme()
        .tokenrepository(persistenttokenrepository())//tokenrepository,登錄成功后往數據庫存token的
        .tokenvalidityseconds(securityproperties.getbrowser().getremembermeseconds())//記住我秒數
        .userdetailsservice(userdetailsservice) //記住我成功后,調用userdetailsservice查詢用戶信息
      .and()
      //授權相關的配置
      .authorizerequests()
        // /authentication/require:處理登錄,securityproperties.getbrowser().getloginpage():用戶配置的登錄頁
        .antmatchers("/authentication/require",
        securityproperties.getbrowser().getloginpage(),//放過登錄頁不過濾,否則報錯
        "/verifycode/image").permitall() //驗證碼
        .anyrequest()    //任何請求
        .authenticated()  //都需要身份認證
      .and()
        .csrf().disable() //關閉csrf防護
      
  }
}ecurity.browser;

其中由于要和數據庫打交道,所以需要注入一個數據源:application.properties

?
1
2
3
4
spring.datasource.driver-class-name=com.mysql.jdbc.driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/imooc-demo
spring.datasource.username=root
spring.datasource.password=root

啟動應用,訪問 localhost:8080/user,需要登錄

Spring Security 構建rest服務實現rememberme 記住我功能

Spring Security 構建rest服務實現rememberme 記住我功能

登錄成功:

Spring Security 構建rest服務實現rememberme 記住我功能

數據庫:生成一個persistent_logins表,存進去了一條數據

Spring Security 構建rest服務實現rememberme 記住我功能

停止服務,從新啟動(注釋掉生成保存token表的jdbctokenrepository.setcreatetableonstartup(true);)因為我們的用戶登錄信息都存在了session中,所以重啟服務后,再訪問localhost:8080/user,本應該重新引導到登錄頁,但是由于配置了記住我,所以能夠直接訪問,拿到了接口數據

Spring Security 構建rest服務實現rememberme 記住我功能

請求頭:

Spring Security 構建rest服務實現rememberme 記住我功能

至此基本的rememberme已做好

完整代碼放在了github:https://github.com/lhy1234/spring-security

總結

以上所述是小編給大家介紹的spring security 構建rest服務實現rememberme 記住我功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://www.cnblogs.com/lihaoyang/archive/2018/03/06/8507889.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产欧美日韩成人 | 亚洲精品国产AV成人毛片 | 成人蜜桃网 | 师尊被各种play打屁股 | 荡女淫春2古装 | 窝窝午夜精品一区二区 | 色图图片| 青青草精品在线观看 | 亚洲福利电影一区二区? | 亚洲成A人片在线观看中文L | 天堂俺去俺来也www久久婷婷 | 亚洲国产剧情中文视频在线 | 91久久碰国产 | 精品一区二区三区色花堂 | 亚洲色图丝袜 | 日本大片免a费观看在线 | 免费观看无遮挡www的小视频 | 暖暖视频日本 | 亚洲人成网站在线观看90影院 | 精品视频中文字幕 | 四虎2023| 九九99靖品 | 99在线播放视频 | chinesexxxxhd人妖 chinesespanking调教 | 日韩精品一区二区三区毛片 | 糖心hd在线观看 | 禁忌第一季第3季 | 美女扒开粉嫩尿口漫画 | 亚洲瑟瑟网 | 色天天综合网色鬼综合 | tobu8中国在线播放免费 | 99爱免费| 国产欧美一区二区精品性色99 | 亚洲AV综合99一二三四区 | 青草草在线 | 青青草久| 91青青视频| 亚洲日韩中文字幕一区 | 青青青久热国产精品视频 | 偷拍综合网| 草久久网 |