spring security的本質
spring security 本質上是一連串的 filter , 然后又以一個獨立的 filter 的形式插入到 filter chain 里,其名為 filterchainproxy 。 如圖所示。
實際上 filterchainproxy 下面可以有多條 filter chain ,來針對不同的url做驗證,而 filter chain 中所擁有的 filter 則會根據定義的服務自動增減。所以無需要顯示再定義這些 filter ,除非想要實現自己的邏輯。
關鍵類
authentication
authentication 是一個接口,用來表示用戶認證信息,在用戶登錄認證之前相關信息會封裝為一個 authentication 具體實現類的對象,在登錄認證成功之后又會生成一個信息更全面,包含用戶權限等信息的 authentication 對象,然后把它保存在 securitycontextholder 所持有的 securitycontext 中,供后續的程序進行調用,如訪問權限的鑒定等。
authenticationmanager
用來做驗證的最主要的接口為 authenticationmanager ,這個接口只有一個方法:
1
2
3
4
|
public interface authenticationmanager { authentication authenticate(authentication authentication) throws authenticationexception; } |
其中 authenticate() 方法運行后可能會有三種情況:
驗證成功,返回一個帶有用戶信息的 authentication 。
驗證失敗,拋出一個 authenticationexception 異常。
無法判斷,返回 null 。
providermanager
providermanager 是上面的 authenticationmanager 最常見的實現,它不自己處理驗證,而是將驗證委托給其所配置的 authenticationprovider 列表,然后會依次調用每一個 authenticationprovider 進行認證,這個過程中只要有一個 authenticationprovider 驗證成功,就不會再繼續做更多驗證,會直接以該認證結果作為 providermanager 的認證結果。
認證過程
用戶使用用戶名和密碼進行登錄。
spring security 將獲取到的用戶名和密碼封裝成一個 authentication 接口的實現類,比如常用的 usernamepasswordauthenticationtoken 。
將上述產生的 authentication 對象傳遞給 authenticationmanager 的實現類 providermanager 進行認證。
providermanager 依次調用各個 authenticationprovider 進行認證,認證成功后返回一個封裝了用戶權限等信息的 authentication 對象。
將 authenticationmanager 返回的 authentication 對象賦予給當前的 securitycontext 。
自定義驗證
有了以上的知識儲備后就可以來自定義驗證方法了。通過上面可以看出,實際上真正來做驗證操作的是一個個的 authenticationprovider ,所以如果要自定義驗證方法,只需要實現一個自己的 authenticationprovider 然后再將其添加進 providermanager 里就行了。
自定義authenticationprovider
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@component public class customauthenticationprovider implements authenticationprovider { @override public authentication authenticate(authentication authentication) throws authenticationexception { string name = authentication.getname(); string password = authentication.getcredentials().tostring(); if (shouldauthenticateagainstthirdpartysystem()) { // use the credentials // and authenticate against the third-party system return new usernamepasswordauthenticationtoken( name, password, new arraylist<>()); } else { return null ; } } @override public boolean supports( class <?> authentication) { return authentication.equals( usernamepasswordauthenticationtoken. class ); } } |
其中的 supports() 方法接受一個 authentication 參數,用來判斷傳進來的 authentication 是不是該 authenticationprovider 能夠處理的類型。
注冊authenticationprovider
現在再將剛創建的 authenticationprovider 在 與providermanager 里注冊,所有操作就完成了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@configuration @enablewebsecurity @componentscan ( "org.baeldung.security" ) public class securityconfig extends websecurityconfigureradapter { @autowired private customauthenticationprovider authprovider; @override protected void configure( authenticationmanagerbuilder auth) throws exception { auth.authenticationprovider(authprovider); } @override protected void configure(httpsecurity http) throws exception { http.authorizerequests().anyrequest().authenticated() .and() .httpbasic(); } } |
總結
以上所述是小編給大家介紹的spring security驗證流程剖析及自定義驗證方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/xz816111/p/8528896.html