溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Security如何自定義驗證方法

發(fā)布時間:2021-08-13 12:35:42 來源:億速云 閱讀:377 作者:小新 欄目:編程語言

這篇文章主要介紹Spring Security如何自定義驗證方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

Spring Security的本質(zhì)

Spring Security 本質(zhì)上是一連串的 Filter , 然后又以一個獨立的 Filter 的形式插入到 Filter Chain 里,其名為 FilterChainProxy 。 如圖所示。

Spring Security如何自定義驗證方法 

實際上 FilterChainProxy 下面可以有多條 Filter Chain ,來針對不同的URL做驗證,而 Filter Chain 中所擁有的 Filter 則會根據(jù)定義的服務(wù)自動增減。所以無需要顯示再定義這些 Filter ,除非想要實現(xiàn)自己的邏輯。

Spring Security如何自定義驗證方法 

關(guān)鍵類

Authentication

Authentication 是一個接口,用來表示用戶認證信息,在用戶登錄認證之前相關(guān)信息會封裝為一個 Authentication 具體實現(xiàn)類的對象,在登錄認證成功之后又會生成一個信息更全面,包含用戶權(quán)限等信息的 Authentication 對象,然后把它保存在 SecurityContextHolder 所持有的 SecurityContext 中,供后續(xù)的程序進行調(diào)用,如訪問權(quán)限的鑒定等。

AuthenticationManager

用來做驗證的最主要的接口為 AuthenticationManager ,這個接口只有一個方法:

public interface AuthenticationManager {
 Authentication authenticate(Authentication authentication)
 throws AuthenticationException;
}

其中 authenticate() 方法運行后可能會有三種情況:

驗證成功,返回一個帶有用戶信息的 Authentication 。

驗證失敗,拋出一個 AuthenticationException 異常。

無法判斷,返回 null 。

ProviderManager

ProviderManager 是上面的 AuthenticationManager 最常見的實現(xiàn),它不自己處理驗證,而是將驗證委托給其所配置的 AuthenticationProvider 列表,然后會依次調(diào)用每一個 AuthenticationProvider 進行認證,這個過程中只要有一個 AuthenticationProvider 驗證成功,就不會再繼續(xù)做更多驗證,會直接以該認證結(jié)果作為 ProviderManager 的認證結(jié)果。

Spring Security如何自定義驗證方法 

認證過程

用戶使用用戶名和密碼進行登錄。

Spring Security 將獲取到的用戶名和密碼封裝成一個 Authentication 接口的實現(xiàn)類,比如常用的 UsernamePasswordAuthenticationToken 。

將上述產(chǎn)生的 Authentication 對象傳遞給 AuthenticationManager 的實現(xiàn)類 ProviderManager 進行認證。

ProviderManager 依次調(diào)用各個 AuthenticationProvider 進行認證,認證成功后返回一個封裝了用戶權(quán)限等信息的 Authentication 對象。

將 AuthenticationManager 返回的 Authentication 對象賦予給當前的 SecurityContext 。

自定義驗證

有了以上的知識儲備后就可以來自定義驗證方法了。通過上面可以看出,實際上真正來做驗證操作的是一個個的 AuthenticationProvider ,所以如果要自定義驗證方法,只需要實現(xiàn)一個自己的 AuthenticationProvider 然后再將其添加進 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ù),用來判斷傳進來的 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();
 }
}

以上是“Spring Security如何自定義驗證方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI