spring security相關(guān)認(rèn)證類詳解

AuthenticationProvider

首先進(jìn)入到AuthenticationProvider源碼中可以看到它只是個(gè)簡(jiǎn)單的接口里面也只有兩個(gè)方法:

public interface AuthenticationProvider {
    // 具體認(rèn)證流程
    Authentication authenticate(Authentication authentication)
            throws AuthenticationException;    
    // supports函數(shù)用來指明該P(yáng)rovider是否適用于該類型的認(rèn)證态罪,如果不合適睹晒,則尋找另一個(gè)Provider進(jìn)行驗(yàn)證處理阿趁。    
    boolean supports(Class<?> authentication);
}

當(dāng)我們沒有指定相關(guān)AuthenticationProvider 對(duì)象時(shí)springSecurity默認(rèn)使用的就時(shí)上圖中的DaoAuthenticationProvider進(jìn)行驗(yàn)證缸剪。也就是最常見的賬戶名密碼的方式惫确。但是實(shí)際開發(fā)中我們往往需要實(shí)現(xiàn)自定義認(rèn)證流程比如最常見的短信驗(yàn)證碼,第三方登錄等等嗦玖。這個(gè)時(shí)候我們就可以通過實(shí)現(xiàn)自己的 AuthenticationProvider方式來進(jìn)行自定義認(rèn)證合武。只需要在WebSecurityConfigurerAdapter適配器類的config方法中加入自己實(shí)現(xiàn)的AuthenticationProvider 即可。

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

說到AuthenticationProvider就離不開AuthenticationManager這個(gè)接口

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

同樣的AuthenticationManager也是一個(gè)頂級(jí)接口胞得,可以看到它其中也定義了一個(gè)跟AuthenticationProvider一摸一樣的方法荧止。如果說AuthenticationProvider 是對(duì)認(rèn)證的具體實(shí)現(xiàn),則AuthenticationManager則是對(duì)我們眾多AuthenticationProvider的一個(gè)統(tǒng)一管理。Authentication Ma nager的實(shí)現(xiàn)有很多跃巡,通常使用ProviderManager對(duì)認(rèn)證請(qǐng)求鏈進(jìn)行管理危号。


從源碼中可以看到,ProviderManager提供了一個(gè)list對(duì)AuthenticationProvider進(jìn)行統(tǒng)一管理素邪,即一個(gè)認(rèn)證處理器鏈來支持同一個(gè)應(yīng)用中的多個(gè)不同身份認(rèn)證機(jī)制葱色,ProviderManager將會(huì)根據(jù)順序來進(jìn)行驗(yàn)證。

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()) {
            // 如果支持認(rèn)證實(shí)現(xiàn)類就繼續(xù)處理
            if (!provider.supports(toTest)) {
                continue;
            }

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

            try {
              // 調(diào)用實(shí)現(xiàn)類的authenticate方法進(jìn)行真實(shí)業(yè)務(wù)邏輯認(rèn)證處理
                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ā)送認(rèn)證成功事件
                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;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末娘香,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子办龄,更是在濱河造成了極大的恐慌烘绽,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俐填,死亡現(xiàn)場(chǎng)離奇詭異安接,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)英融,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門盏檐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驶悟,你說我怎么就攤上這事胡野。” “怎么了痕鳍?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵硫豆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我笼呆,道長(zhǎng)熊响,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任诗赌,我火速辦了婚禮汗茄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铭若。我一直安慰自己洪碳,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布奥喻。 她就那樣靜靜地躺著偶宫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪环鲤。 梳的紋絲不亂的頭發(fā)上纯趋,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼吵冒。 笑死纯命,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的痹栖。 我是一名探鬼主播亿汞,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼揪阿!你這毒婦竟也來了疗我?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤南捂,失蹤者是張志新(化名)和其女友劉穎吴裤,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體溺健,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡麦牺,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鞭缭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剖膳。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖岭辣,靈堂內(nèi)的尸體忽然破棺而出吱晒,到底是詐尸還是另有隱情,我是刑警寧澤沦童,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布枕荞,位于F島的核電站,受9級(jí)特大地震影響搞动,放射性物質(zhì)發(fā)生泄漏躏精。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一鹦肿、第九天 我趴在偏房一處隱蔽的房頂上張望矗烛。 院中可真熱鬧,春花似錦箩溃、人聲如沸瞭吃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽歪架。三九已至,卻和暖如春霹陡,著一層夾襖步出監(jiān)牢的瞬間和蚪,已是汗流浹背止状。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留攒霹,地道東北人怯疤。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像催束,于是被迫代替她去往敵國(guó)和親集峦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容