欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springSecurity之AuthenticationProvider用法解析

 更新時間:2023年03月17日 14:34:25   作者:chihaihai  
這篇文章主要介紹了springSecurity之AuthenticationProvider用法解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

AuthenticationProvider解析

首先進入到AuthenticationProvider源碼中可以看到它只是個簡單的接口里面也只有兩個方法:

public interface AuthenticationProvider {
	// 具體認證流程
	Authentication authenticate(Authentication authentication)
			throws AuthenticationException;	
    // supports函數(shù)用來指明該Provider是否適用于該類型的認證,如果不合適,則尋找另一個Provider進行驗證處理。	
	boolean supports(Class<?> authentication);
}

AuthenticationProvider是用戶自定義身份認證,認證流程頂級接口。

唯一作用即使用來進行身份驗證,同時springSecurity也為我們提供了很多方便的實現(xiàn)類。

在這里插入圖片描述

當我們沒有指定相關(guān)AuthenticationProvider 對象時springSecurity默認使用的就時上圖中的DaoAuthenticationProvider進行驗證。

也就是最常見的賬戶名密碼的方式。但是實際開發(fā)中我們往往需要實現(xiàn)自定義認證流程比如最常見的短信驗證碼,第三方登錄等等。

這個時候我們就可以通過實現(xiàn)自己的 AuthenticationProvider方式來進行自定義認證。

只需要在WebSecurityConfigurerAdapter適配器類的config方法中加入自己實現(xiàn)的AuthenticationProvider 即可。

 @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(authenticationProvider());
    }

說到AuthenticationProvider就離不開AuthenticationManager這個接口

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

同樣的AuthenticationManager也是一個頂級接口,可以看到它其中也定義了一個跟AuthenticationProvider一摸一樣的方法。

如果說Auth ntic ationProvider是對認證的具體實現(xiàn),則AuthenticationManager則是對我們眾多AuthenticationProvider的一個統(tǒng)一管理。

Authentication Ma nager的實現(xiàn)有很多,通常使用ProviderManager對認證請求鏈進行管理。

在這里插入圖片描述

從源碼中可以看到

ProviderManager提供了一個list對AuthenticationProvider進行統(tǒng)一管理,即一個認證處理器鏈來支持同一個應(yīng)用中的多個不同身份認證機制,ProviderManager將會根據(jù)順序來進行驗證。

public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {
		Class<? extends Authentication> toTest = authentication.getClass();
		AuthenticationException lastException = null;
		AuthenticationException parentException = null;
		Authentication result = null;
		Authentication parentResult = null;
		boolean debug = logger.isDebugEnabled();

		for (AuthenticationProvider provider : getProviders()) {
			// 如果支持認證實現(xiàn)類就繼續(xù)處理
			if (!provider.supports(toTest)) {
				continue;
			}

			if (debug) {
				logger.debug("Authentication attempt using "
						+ provider.getClass().getName());
			}

			try {
			  // 調(diào)用實現(xiàn)類的authenticate方法進行真實業(yè)務(wù)邏輯認證處理
				result = provider.authenticate(authentication);

				if (result != null) {
					copyDetails(authentication, result);
					break;
				}
			}
			catch (AccountStatusException e) {
				prepareException(e, authentication);
				// SEC-546: Avoid polling additional providers if auth failure is due to
				// invalid account status
				throw e;
			}
			catch (InternalAuthenticationServiceException e) {
				prepareException(e, authentication);
				throw e;
			}
			catch (AuthenticationException e) {
				lastException = e;
			}
		}

		if (result == null && parent != null) {
			// Allow the parent to try.
			try {
				result = parentResult = parent.authenticate(authentication);
			}
			catch (ProviderNotFoundException e) {
				// ignore as we will throw below if no other exception occurred prior to
				// calling parent and the parent
				// may throw ProviderNotFound even though a provider in the child already
				// handled the request
			}
			catch (AuthenticationException e) {
				lastException = parentException = e;
			}
		}

		if (result != null) {
			if (eraseCredentialsAfterAuthentication
					&& (result instanceof CredentialsContainer)) {
				// Authentication is complete. Remove credentials and other secret data
				// from authentication
				((CredentialsContainer) result).eraseCredentials();
			}

			// If the parent AuthenticationManager was attempted and successful than it will publish an AuthenticationSuccessEvent
			// This check prevents a duplicate AuthenticationSuccessEvent if the parent AuthenticationManager already published it
			if (parentResult == null) {
			  //  //發(fā)送認證成功事件
				eventPublisher.publishAuthenticationSuccess(result);
			}
			return result;
		}

		// Parent was null, or didn't authenticate (or throw an exception).

		if (lastException == null) {
			lastException = new ProviderNotFoundException(messages.getMessage(
					"ProviderManager.providerNotFound",
					new Object[] { toTest.getName() },
					"No AuthenticationProvider found for {0}"));
		}

		// If the parent AuthenticationManager was attempted and failed than it will publish an AbstractAuthenticationFailureEvent
		// This check prevents a duplicate AbstractAuthenticationFailureEvent if the parent AuthenticationManager already published it
		if (parentException == null) {
			prepareException(lastException, authentication);
		}

		throw lastException;
	}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • drools中then部分的寫法(推薦)

    drools中then部分的寫法(推薦)

    本文介紹一下drools中then部分的寫法,以及一些內(nèi)置的方法,比如insert/delete/modify等等。同時也介紹一下rule的繼承,和在when中實現(xiàn)if else if?等操作,感興趣的朋友跟隨小編一起看看吧
    2022-05-05
  • 深入理解Java中的HashMap的實現(xiàn)機制

    深入理解Java中的HashMap的實現(xiàn)機制

    這篇文章主要介紹了深入理解Java中的HashMap的實現(xiàn)機制,同時也有助于理解Java中對于哈希函數(shù)的相關(guān)處理方式,需要的朋友可以參考下
    2015-07-07
  • springcloud本地配置優(yōu)先方式

    springcloud本地配置優(yōu)先方式

    這篇文章主要介紹了springcloud本地配置優(yōu)先方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Springboot整合freemarker和相應(yīng)的語法詳解

    Springboot整合freemarker和相應(yīng)的語法詳解

    FreeMarker是一款Spring官方推薦使用的模板引擎。接下來通過本文給大家介紹Springboot整合freemarker和相應(yīng)的語法,感興趣的朋友一起看看吧
    2021-09-09
  • Java實現(xiàn)屏幕截圖及剪裁

    Java實現(xiàn)屏幕截圖及剪裁

    這是一篇入門級文章,高手請略過。在這篇文章中我們將學(xué)習(xí)如何用 Java 對圖像進行剪裁并將剪裁出來的部分單獨保存到文件中。
    2014-09-09
  • java多線程實現(xiàn)下載圖片并壓縮

    java多線程實現(xiàn)下載圖片并壓縮

    這篇文章主要為大家詳細介紹了java多線程實現(xiàn)下載圖片并壓縮,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 淺談Spring Session工作原理

    淺談Spring Session工作原理

    Spring Session是為了解決多進程session共享的問題,本文將介紹怎么使用Spring Session,以及Spring Session工作原理
    2021-06-06
  • 學(xué)好Java?MyBatis攔截器,提高工作效率

    學(xué)好Java?MyBatis攔截器,提高工作效率

    這篇文章主要介紹了Java中的?MyBatis攔截器,??Mybatis攔截器設(shè)計的初衷就是為了供用戶在某些時候可以實現(xiàn)自己的邏輯而不必去動Mybatis固有的邏輯。詳細內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容,希望對你有所幫助
    2022-02-02
  • SpringBoot的@Conditional條件注解詳解

    SpringBoot的@Conditional條件注解詳解

    這篇文章主要介紹了SpringBoot的@Conditional條件注解詳解,打開每個自動配置類,都會看到@Conditional或其衍生的條件注解,本節(jié)我們來認識下@Conditional注解,需要的朋友可以參考下
    2023-12-12
  • SpringBoot?Http遠程調(diào)用的方法

    SpringBoot?Http遠程調(diào)用的方法

    這篇文章主要為大家詳細介紹了SpringBoot?Http遠程調(diào)用的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論