UsernamePasswordAuthenticationFilter 源碼分析

UsernamePasswordAuthenticationFilterAbstractAuthenticationProcessingFilter 的子類,主要作用是對用戶身份信息的驗(yàn)證。
  關(guān)于 AbstractAuthenticationProcessingFilter 的分析見此: AbstractAuthenticationProcessingFilter 源碼分析。

繼承關(guān)系

UsernamePasswordAuthenticationFilter 繼承自AbstractAuthenticationProcessingFilter。

public class UsernamePasswordAuthenticationFilter extends
        AbstractAuthenticationProcessingFilter {

AbstractAuthenticationProcessingFilter 是處理 form 登陸的過濾器,與 form 登陸有關(guān)的所有操作都是在該類中及其子類中進(jìn)行的。

流程分析

關(guān)于 UsernamePasswordAuthenticationFilter 處理請求的大體流程和其父類一致,見此: AbstractAuthenticationProcessingFilter 源碼分析。該處主要分析UsernamePasswordAuthenticationFilter 實(shí)現(xiàn)的父類方法 attemptAuthentication() 中的用戶身份驗(yàn)證邏輯。
  attemptAuthentication() 方法代碼如下所示:

public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException {
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }

-->1    String username = obtainUsername(request);
-->2    String password = obtainPassword(request);

        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        username = username.trim();

-->3    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
                username, password);

        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);

-->4    return this.getAuthenticationManager().authenticate(authRequest);
    }
  • -->1-->2 處的代碼從 request 中提取用戶名和密碼。
  • -->3 處代碼為構(gòu)建類UsernamePasswordAuthenticationToken 成員變量,UsernamePasswordAuthenticationToken 是一個(gè)用戶信息的載體類,用來存儲(chǔ)及傳遞用戶名和用戶密碼。
  • -->4 處代碼調(diào)用getAuthenticationManager()方法獲取AuthenticationManager實(shí)例,getAuthenticationManager()方法定義如下:
protected AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

然后調(diào)用獲取到的AuthenticationManager實(shí)例的authenticate()方法對封裝在authRequest [UsernamePasswordAuthenticationToken]中的用戶名及密碼進(jìn)行驗(yàn)證。

注意: AuthenticationManager這里可以理解為 spring security 配置文件中 <authentication-manager/> 的實(shí)現(xiàn)類。

attemptAuthentication 驗(yàn)證函數(shù)流程圖.png

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,659評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,282評(píng)論 6 342
  • 閃耀是自信嗎 奪目是自信嗎 爭搶是自信嗎 無禮是自信嗎 嘩眾取寵是自信嗎 毫不收斂是自信嗎 偽裝是自信嗎 赤裸是自...
    天籟村閱讀 163評(píng)論 0 0
  • 地址 傳送門 說明 readline模塊提供了一個(gè)接口,從readable流讀取一行數(shù)據(jù),比如process.st...
    一溪酒閱讀 641評(píng)論 0 0
  • 好久不見,2016年第一篇,最近正在和團(tuán)隊(duì)的小伙伴一起處理一個(gè)復(fù)雜的表單設(shè)計(jì),正好看到這篇文章,小有啟發(fā),就決定譯...
    TerryFan閱讀 2,067評(píng)論 2 7

友情鏈接更多精彩內(nèi)容