spring security記住我基本原理:
登錄的時候,請求發送給過濾器usernamepasswordauthenticationfilter,當該過濾器認證成功后,會調用remembermeservice,會生成一個token,將token寫入到瀏覽器cookie,同時remembermeservice里邊還有個tokenrepository,將token和用戶信息寫入到數據庫中。這樣當用戶再次訪問系統,訪問某一個接口時,會經過一個remembermeauthenticationfilter的過濾器,他會讀取cookie中的token,交給rememberservice,rememberservice會用tokenrepository根據token從數據庫中查是否有記錄,如果有記錄會把用戶名取出來,再調用userdetailservice根據用戶名獲取用戶信息,然后放在securitycontext里。
remembermeauthenticationfilter
在spring security中認證過濾器鏈的倒數第二個過濾器位置,當其他認證過濾器都沒法認證成功的時候,就會調用remembermeauthenticationfilter嘗試認證。
實現:
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,需要登錄
登錄成功:
數據庫:生成一個persistent_logins表,存進去了一條數據
停止服務,從新啟動(注釋掉生成保存token表的jdbctokenrepository.setcreatetableonstartup(true);
)因為我們的用戶登錄信息都存在了session中,所以重啟服務后,再訪問localhost:8080/user,本應該重新引導到登錄頁,但是由于配置了記住我,所以能夠直接訪問,拿到了接口數據
請求頭:
至此基本的rememberme已做好
完整代碼放在了github:https://github.com/lhy1234/spring-security
總結
以上所述是小編給大家介紹的spring security 構建rest服務實現rememberme 記住我功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/lihaoyang/archive/2018/03/06/8507889.html