Spring Security驗(yàn)證流程剖析及自定義驗(yàn)證方法
Spring Security的本質(zhì)
Spring Security 本質(zhì)上是一連串的 Filter , 然后又以一個獨(dú)立的 Filter 的形式插入到 Filter Chain 里,其名為 FilterChainProxy 。 如圖所示。
實(shí)際上 FilterChainProxy 下面可以有多條 Filter Chain ,來針對不同的URL做驗(yàn)證,而 Filter Chain 中所擁有的 Filter 則會根據(jù)定義的服務(wù)自動增減。所以無需要顯示再定義這些 Filter ,除非想要實(shí)現(xiàn)自己的邏輯。
關(guān)鍵類
Authentication
Authentication 是一個接口,用來表示用戶認(rèn)證信息,在用戶登錄認(rèn)證之前相關(guān)信息會封裝為一個 Authentication 具體實(shí)現(xiàn)類的對象,在登錄認(rèn)證成功之后又會生成一個信息更全面,包含用戶權(quán)限等信息的 Authentication 對象,然后把它保存在 SecurityContextHolder 所持有的 SecurityContext 中,供后續(xù)的程序進(jìn)行調(diào)用,如訪問權(quán)限的鑒定等。
AuthenticationManager
用來做驗(yàn)證的最主要的接口為 AuthenticationManager ,這個接口只有一個方法:
public interface AuthenticationManager { Authentication authenticate(Authentication authentication) throws AuthenticationException; }
其中 authenticate() 方法運(yùn)行后可能會有三種情況:
驗(yàn)證成功,返回一個帶有用戶信息的 Authentication 。
驗(yàn)證失敗,拋出一個 AuthenticationException 異常。
無法判斷,返回 null 。
ProviderManager
ProviderManager 是上面的 AuthenticationManager 最常見的實(shí)現(xiàn),它不自己處理驗(yàn)證,而是將驗(yàn)證委托給其所配置的 AuthenticationProvider 列表,然后會依次調(diào)用每一個 AuthenticationProvider 進(jìn)行認(rèn)證,這個過程中只要有一個 AuthenticationProvider 驗(yàn)證成功,就不會再繼續(xù)做更多驗(yàn)證,會直接以該認(rèn)證結(jié)果作為 ProviderManager 的認(rèn)證結(jié)果。
認(rèn)證過程
用戶使用用戶名和密碼進(jìn)行登錄。
Spring Security 將獲取到的用戶名和密碼封裝成一個 Authentication 接口的實(shí)現(xiàn)類,比如常用的 UsernamePasswordAuthenticationToken 。
將上述產(chǎn)生的 Authentication 對象傳遞給 AuthenticationManager 的實(shí)現(xiàn)類 ProviderManager 進(jìn)行認(rèn)證。
ProviderManager 依次調(diào)用各個 AuthenticationProvider 進(jìn)行認(rèn)證,認(rèn)證成功后返回一個封裝了用戶權(quán)限等信息的 Authentication 對象。
將 AuthenticationManager 返回的 Authentication 對象賦予給當(dāng)前的 SecurityContext 。
自定義驗(yàn)證
有了以上的知識儲備后就可以來自定義驗(yàn)證方法了。通過上面可以看出,實(shí)際上真正來做驗(yàn)證操作的是一個個的 AuthenticationProvider ,所以如果要自定義驗(yàn)證方法,只需要實(shí)現(xiàn)一個自己的 AuthenticationProvider 然后再將其添加進(jìn) ProviderManager 里就行了。
自定義AuthenticationProvider
@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 參數(shù),用來判斷傳進(jìn)來的 authentication 是不是該 AuthenticationProvider 能夠處理的類型。
注冊AuthenticationProvider
現(xiàn)在再將剛創(chuàng)建的 AuthenticationProvider 在 與ProviderManager 里注冊,所有操作就完成了。
@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(); } }
總結(jié)
以上所述是小編給大家介紹的Spring Security驗(yàn)證流程剖析及自定義驗(yàn)證方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Springboot如何使用Aspectj實(shí)現(xiàn)AOP面向切面編程
這篇文章主要介紹了Springboot如何使用Aspectj實(shí)現(xiàn)AOP面向切面編程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01詳解java.lang.reflect.Modifier.isInterface()方法
這篇文章主要介紹了詳解java.lang.reflect.Modifier.isInterface()方法的相關(guān)資料,這里提供實(shí)例幫助大家理解這個方法的使用,需要的朋友可以參考下2017-09-09Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例
下面小編就為大家分享一篇Java創(chuàng)建二叉搜索樹,實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例,具有很好的參考價值,希望對大家有所幫助2017-12-12Java中利用BitMap位圖實(shí)現(xiàn)海量級數(shù)據(jù)去重
有許多方法可以用來去重,比如使用列表、集合等等,但這些方法通常只適用于一般情況,然而,當(dāng)涉及到大量數(shù)據(jù)去重時,常見的 Java Set、List,甚至是 Java 8 的新特性 Stream 流等方式就顯得不太合適了,本文給大家介紹了Java中利用BitMap位圖實(shí)現(xiàn)海量級數(shù)據(jù)去重2024-04-04Java實(shí)戰(zhàn)之藥品管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了利用Java實(shí)現(xiàn)的藥品管理系統(tǒng),本項(xiàng)目屬于前后端分離的項(xiàng)目,分為兩個角色藥品管理員和取藥處人員,感興趣的小伙伴可以學(xué)習(xí)一下2022-04-04JDK9為何要將String的底層實(shí)現(xiàn)由char[]改成了byte[]
String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下2022-03-03Java servlet通過事件驅(qū)動進(jìn)行高性能長輪詢詳解
這篇文章主要介紹了基于servlet3.0+事件驅(qū)動實(shí)現(xiàn)高性能長輪詢的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2022-06-06