激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 免费看性xxx高清视频自由 | 国产99视频精品免视看9 | 久久吊| 国产女做a爱免费视频 | 二区三区四区 | 日韩av有码在线 | 久久久久性 | 91精品最新国内在线播放 | 日本欧美一区二区 | 国产精品久久久久久久久久妇女 | 久久久日韩精品一区二区 | 精品一区二区三区欧美 | a黄网站 | 精品xxxx户外露出视频 | 午夜人体 | 成人福利免费在线观看 | 毛片免费视频播放 | 精品不卡| 久久久久久中文字幕 | 精品成人在线 | 成人免费激情视频 | 国产精品视频一区二区噜噜 | 国内精品一级毛片免费看 | 操操操操网 | 精国产品一区二区三区四季综 | 久久国产不卡 | 免费网站看v片在线a | 亚洲最新色| 国产精品久久久久久久午夜片 | 久久久久久久爱 | 精品国产欧美一区二区 | 日韩精品一区二区在线播放 | 国产免费一区二区三区最新不卡 | 91精品国产91久久久久久丝袜 | av播播| 国产69久久精品成人看 | 国av在线| 久久精品免费国产 | 国产精品久久久久久久久岛 | 欧美国产精品久久 | 久久亚洲精品视频 |