Spring?Security中如何獲取AuthenticationManager對(duì)象
有時(shí)需要使用AuthenticationManager(以下簡稱Manager)對(duì)象,可是這個(gè)對(duì)象不是Bean,沒有直接保存在Spring的Bean庫中。那么如何獲取Spring Security中的這個(gè)對(duì)象呢?
在Spring Security 5.7.0-M2之前,通常會(huì)擴(kuò)展WebSecurityConfigurerAdapter(以下簡稱Adapter)類來自定義網(wǎng)絡(luò)安全配置。Adapter類中有一個(gè)方法authenticationManager()可以提供Manager對(duì)象。但是從Spring Security 5.7.0-M2開始,Adapter類就被棄用,再用此類中的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類的私有字段authenticationConfiguration的getAuthenticationManager()方法獲取Manager對(duì)象。而字段authenticationConfiguration是使用方法setAthenticationConfiguration()自動(dòng)注入的,所以Spring的Bean庫中存在Manager對(duì)象。由此可得到如下獲取AuthenticationManager對(duì)象的方案一。
方案一:從Spring的Bean庫中獲取AuthenticationConfiguration(以下簡稱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ì)象中沒有Manager對(duì)象,則從Spring的Bean庫中獲取AuthenticationManagerBuilder(以下簡稱Builder)對(duì)象。
③ 如果已經(jīng)用Builder對(duì)象構(gòu)建了Manager對(duì)象,則返回一個(gè)使用Builder對(duì)象初始化的AuthenticationManagerDelegator對(duì)象。
④ AuthenticationManagerDelegator是Manager的子類。該類代理了另一個(gè)Manager對(duì)象,被代理的Manager對(duì)象是使用Builder對(duì)象的getObject()方法獲得的。Builder#getObject()的代碼表明,在調(diào)用該方法前,需要先在Builder對(duì)象內(nèi)構(gòu)建一個(gè)Manager。也就是說可以從Spring的Bean庫中獲取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是通過方法setGlobalAuthConfigurers()自動(dòng)注入的)。
⑥ 然后使用Builder對(duì)象的build()方法構(gòu)建Manager對(duì)象。
⑦ 在Builder#build()方法中,第一次調(diào)用該方法會(huì)使用doBuild()方法生成Manager對(duì)象(不分析doBuild()方法),并將這個(gè)對(duì)象緩存下來。以后再調(diào)用build()方法將會(huì)拋出AlreadyBuiltException異常。也就是說可以從Spring的Bean庫中獲取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庫中獲取AuthenticationManagerBuilder對(duì)象,然后使用該對(duì)象的build()方法或getObject()方法獲取AuthenticationManager對(duì)象。獲取AuthenticationManager對(duì)象的方案二如下。
方案二:從Spring的Bean庫中獲取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ì)象做了處理,而方案二并沒有做這種處理,推薦使用方案一。
到此這篇關(guān)于在Spring Security中如何獲取AuthenticationManager對(duì)象的文章就介紹到這了,更多相關(guān)Spring Security 獲取AuthenticationManager對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解最簡單易懂的Spring Security 身份認(rèn)證流程講解
這篇文章主要介紹了詳解最簡單易懂的Spring Security 身份認(rèn)證流程講解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
springboot使用Hutool的JschUtil及下載安裝步驟
這篇文章主要為大家介紹了springboot使用Hutool的JschUtil的方法及下載安裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
Spring Boot與Prometheus的整合可以實(shí)現(xiàn)對(duì)Spring Boot應(yīng)用的實(shí)時(shí)監(jiān)控,有助于更好地維護(hù)應(yīng)用的性能,本文給大家介紹springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟,感興趣的朋友跟隨小編一起看看吧2024-11-11
基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-10-10
springboot+element-ui實(shí)現(xiàn)多文件一次上傳功能
這篇文章主要介紹了springboot+element-ui多文件一次上傳功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

