国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人h免费观看视频 | 日韩免费一区二区 | 先锋影音av资源站 | 亚洲欧美日韩精品 | 亚洲精品久久久一区二区三区 | 99精品欧美一区二区蜜桃免费 | 精品成人一区二区 | 日本日韩中文字幕 | 亚洲视频在线观看免费 | 亚洲视频在线免费观看 | 亚洲人人射 | 羞羞视频免费观看 | 中文字幕色站 | 亚洲精品综合中文字幕 | av毛片在线 | 午夜精品久久久久久久久 | 亚洲伊人成人 | 亚洲欧洲av在线 | 国内精品久久久久久中文字幕 | 精品久久亚洲 | 99伊人| 亚洲综合色网 | 久久丁香 | 日日撸| av在线一区二区 | 久久男人的天堂 | 五月婷婷综合激情网 | 欧美精品一区二区视频 | 日韩欧美不卡 | 精产国产伦理一二三区 | 亚洲欧美日韩精品久久亚洲区 | 国产不卡免费视频 | 国产精品99久久久久久动医院 | 国产日韩视频 | 亚洲一区在线日韩在线深爱 | 午夜高清视频 | 国产欧美精品一区二区色综合 | 成人国产精品156免费观看 | 亚洲视频在线播放 | 国产成人黄色 | 中文字幕不卡 |