最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Security中如何獲取AuthenticationManager對(duì)象

 更新時(shí)間:2022年11月25日 08:26:36   作者:Halloworlds  
有時(shí)需要使用AuthenticationManager(以下簡(jiǎn)稱(chēng)Manager)對(duì)象,可是這個(gè)對(duì)象不是Bean,沒(méi)有直接保存在Spring的Bean庫(kù)中,那么如何獲取Spring Security中的這個(gè)對(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)文章

最新評(píng)論

西林县| 上杭县| 高州市| 都江堰市| 萨嘎县| 吉林市| 新密市| 永定县| 丹江口市| 安化县| 深圳市| 大足县| 壤塘县| 且末县| 宣汉县| 合作市| 虞城县| 葫芦岛市| 芜湖县| 定边县| 乌兰察布市| 寻乌县| 民县| 台山市| 新巴尔虎右旗| 溧水县| 麦盖提县| 浦北县| 体育| 临澧县| 新河县| 卫辉市| 灵石县| 临朐县| 海原县| 龙川县| 南华县| SHOW| 屏南县| 平江县| 民勤县|