Spring?Security中如何獲取AuthenticationManager對(duì)象
有時(shí)需要使用AuthenticationManager(以下簡(jiǎn)稱(chēng)Manager)對(duì)象,可是這個(gè)對(duì)象不是Bean,沒(méi)有直接保存在Spring的Bean庫(kù)中。那么如何獲取Spring Security中的這個(gè)對(duì)象呢?
在Spring Security 5.7.0-M2之前,通常會(huì)擴(kuò)展WebSecurityConfigurerAdapter(以下簡(jiǎn)稱(chēng)Adapter)類(lèi)來(lái)自定義網(wǎng)絡(luò)安全配置。Adapter類(lèi)中有一個(gè)方法authenticationManager()可以提供Manager對(duì)象。但是從Spring Security 5.7.0-M2開(kāi)始,Adapter類(lèi)就被棄用,再用此類(lèi)中的authenticationManager()方法獲取Manager對(duì)象就不合適了。
以下是Adapter#authenticationManager()方法的源碼。
protected AuthenticationManager authenticationManager() throws Exception {
if (!this.authenticationManagerInitialized) {
configure(this.localConfigureAuthenticationBldr);
if (this.disableLocalConfigureAuthenticationBldr) {
this.authenticationManager = this.authenticationConfiguration.getAuthenticationManager();
} else {
this.authenticationManager = this.localConfigureAuthenticationBldr.build();
}
this.authenticationManagerInitialized = true;
}
return this.authenticationManager;
}可以發(fā)現(xiàn)在該方法中使用Adapter類(lèi)的私有字段authenticationConfiguration的getAuthenticationManager()方法獲取Manager對(duì)象。而字段authenticationConfiguration是使用方法setAthenticationConfiguration()自動(dòng)注入的,所以Spring的Bean庫(kù)中存在Manager對(duì)象。由此可得到如下獲取AuthenticationManager對(duì)象的方案一。
方案一:從Spring的Bean庫(kù)中獲取AuthenticationConfiguration(以下簡(jiǎn)稱(chēng)Configuration)對(duì)象,然后使用Configuration對(duì)象的getAuthenticationManager()方法獲取Manager對(duì)象。(關(guān)于如何從Spring中獲取Bean,本文不作介紹,請(qǐng)讀者自行查閱資料了解)
繼續(xù)查看Configuration#getAuthenticationManager()方法的源碼。
public AuthenticationManager getAuthenticationManager() throws Exception {
if (this.authenticationManagerInitialized) {
return this.authenticationManager;
}
AuthenticationManagerBuilder authBuilder = this.applicationContext.getBean(AuthenticationManagerBuilder.class);
if (this.buildingAuthenticationManager.getAndSet(true)) {
return new AuthenticationManagerDelegator(authBuilder);
}
for (GlobalAuthenticationConfigurerAdapter config : this.globalAuthConfigurers) {
authBuilder.apply(config);
}
this.authenticationManager = authBuilder.build();
if (this.authenticationManager == null) {
this.authenticationManager = getAuthenticationManagerBean();
}
this.authenticationManagerInitialized = true;
return this.authenticationManager;
}源碼中展示的流程如下。
① 如果Configuration對(duì)象中已保存有Manager對(duì)象,則返回該對(duì)象。
② 如果Configuration對(duì)象中沒(méi)有Manager對(duì)象,則從Spring的Bean庫(kù)中獲取AuthenticationManagerBuilder(以下簡(jiǎn)稱(chēng)Builder)對(duì)象。
③ 如果已經(jīng)用Builder對(duì)象構(gòu)建了Manager對(duì)象,則返回一個(gè)使用Builder對(duì)象初始化的AuthenticationManagerDelegator對(duì)象。
④ AuthenticationManagerDelegator是Manager的子類(lèi)。該類(lèi)代理了另一個(gè)Manager對(duì)象,被代理的Manager對(duì)象是使用Builder對(duì)象的getObject()方法獲得的。Builder#getObject()的代碼表明,在調(diào)用該方法前,需要先在Builder對(duì)象內(nèi)構(gòu)建一個(gè)Manager。也就是說(shuō)可以從Spring的Bean庫(kù)中獲取Builder對(duì)象,然后用它的getObject()方法獲得Manager對(duì)象。
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (this.delegate != null) {
return this.delegate.authenticate(authentication);
}
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
this.delegate = this.delegateBuilder.getObject();
this.delegateBuilder = null;
}
}
return this.delegate.authenticate(authentication);
}⑤ 如果尚未使用Builder對(duì)象構(gòu)建Manager對(duì)象,則先把Configuration的私有字段globalAuthConfigurers的數(shù)據(jù)應(yīng)用在Builder對(duì)象上(私有字段globalAuthConfigurers是通過(guò)方法setGlobalAuthConfigurers()自動(dòng)注入的)。
⑥ 然后使用Builder對(duì)象的build()方法構(gòu)建Manager對(duì)象。
⑦ 在Builder#build()方法中,第一次調(diào)用該方法會(huì)使用doBuild()方法生成Manager對(duì)象(不分析doBuild()方法),并將這個(gè)對(duì)象緩存下來(lái)。以后再調(diào)用build()方法將會(huì)拋出AlreadyBuiltException異常。也就是說(shuō)可以從Spring的Bean庫(kù)中獲取Builder對(duì)象,然后用它的build()方法獲得Manager對(duì)象。
@Override
public final O build() throws Exception {
if (this.building.compareAndSet(false, true)) {
this.object = doBuild();
return this.object;
}
throw new AlreadyBuiltException("This object has already been built");
}⑧ 如果Builder對(duì)象未能成功構(gòu)建Manager對(duì)象,則使用Configuration的私有方法lazyBean()方法獲取Manager對(duì)象(不分析lazyBean()方法)。
上述流程表明,可以從Spring的Bean庫(kù)中獲取AuthenticationManagerBuilder對(duì)象,然后使用該對(duì)象的build()方法或getObject()方法獲取AuthenticationManager對(duì)象。獲取AuthenticationManager對(duì)象的方案二如下。
方案二:從Spring的Bean庫(kù)中獲取AuthenticationManagerBuilder對(duì)象,首先調(diào)用該對(duì)象的build()方法獲取Manager對(duì)象,并對(duì)方法調(diào)用捕獲AlreadyBuiltException異常。若捕獲到異常,則在異常處理代碼中調(diào)用Builder對(duì)象的getObject()方法獲取Manager對(duì)象。
方案二可能有缺陷。在Configuration#getAuthenticationManager()方法的源碼中可以看到步驟⑤對(duì)Builder對(duì)象對(duì)象做了處理,而方案二并沒(méi)有做這種處理,推薦使用方案一。
到此這篇關(guān)于在Spring Security中如何獲取AuthenticationManager對(duì)象的文章就介紹到這了,更多相關(guān)Spring Security 獲取AuthenticationManager對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java學(xué)習(xí)之JVM運(yùn)行時(shí)常量池理解
這篇文章主要介紹了java學(xué)習(xí)之JVM運(yùn)行時(shí)常量池理解,對(duì)常量池的好處以及基本類(lèi)型的包裝類(lèi)常量池等作了簡(jiǎn)要分析,有需要的朋友可以借鑒參考下2021-09-09
java公眾平臺(tái)通用接口工具類(lèi)HttpConnectUtil實(shí)例代碼
下面小編就為大家分享一篇java公眾平臺(tái)通用接口工具類(lèi)HttpConnectUtil實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java集合List和Map互轉(zhuǎn)的方法總結(jié)
有時(shí)候我們需要將給定的List轉(zhuǎn)換為Map,或者M(jìn)ap轉(zhuǎn)換為L(zhǎng)ist,本文主要介紹了Java集合List和Map互轉(zhuǎn)的方法總結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
阿里的Easyexcel讀取Excel文件的方法(最新版本)
這篇文章主要介紹了阿里的Easyexcel讀取Excel文件(最新版本)的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
springboot集成測(cè)試最小化依賴(lài)實(shí)踐示例
這篇文章主要為大家介紹了springboot集成測(cè)試最小化依賴(lài)實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解Java?ThreadPoolExecutor的拒絕策略
這篇文章主要介紹了Java?ThreadPoolExecutor的拒絕策略,本文對(duì)于線(xiàn)程的池的幾種策略進(jìn)行詳細(xì)的講解,在實(shí)際的生產(chǎn)中需要集合相關(guān)的場(chǎng)景來(lái)選擇合適的拒絕策略,需要的朋友可以參考下2022-08-08

