SpringBoot + Spring Security 學習筆記(二)安全認證流程源碼詳解

用戶認證流程

UsernamePasswordAuthenticationFilter

咱們直接來看UsernamePasswordAuthenticationFilter類,html

public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        // 判斷是不是 POST 請求
        if (postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                "Authentication method not supported: " + request.getMethod());
        }
        
        // 獲取請求中的用戶,密碼。
        // 就是最簡單的:request.getParameter(xxx)
        String username = obtainUsername(request);
        String password = obtainPassword(request);

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

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

        username = username.trim();

        // 生成 authRequest,本質就是個 usernamePasswordAuthenticationToken
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
            username, password);

        // 把 request 請求也一同塞進 token 裏
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        // 將 authRequest 塞進 AuthenticationManager並返回
        return this.getAuthenticationManager().authenticate(authRequest);
    }
}
複製代碼

attemptAuthentication()方法中:主要是先進行請求判斷並獲取usernamepassword的值,而後再生成一個UsernamePasswordAuthenticationToken對象,將這個對象塞進AuthenticationManager對象並返回,注意:此時的authRequest的權限是沒有任何值的java

UsernamePasswordAuthenticationToken

不過咱們能夠先看看UsernamePasswordAuthenticationToken的構造方法:git

public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
    super(null);
    this.principal = principal;
    this.credentials = credentials;
    setAuthenticated(false);
}
複製代碼

其實UsernamePasswordAuthenticationToken是繼承於Authentication,該對象在學習筆記一中的中"自定義處理登陸成功/失敗"章節裏的自定義登陸成功裏有提到過,它是處理登陸成功回調方法中的一個參數,裏面包含了用戶信息、請求信息等參數。github

來一張繼承關係圖,對其有個大概的認識,注意到Authentication繼承了Principalspring

AuthenticationManager

AuthenticationManager是一個接口,它的全部實現類如圖:api

其中一個十分核心的類就是:ProviderManager,在attemptAuthentication()方法最後返回的就是這個類緩存

this.getAuthenticationManager().authenticate(authRequest);
複製代碼

進入authenticate()方法查看具體作了什麼:安全

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()) {
        // 1.判斷是否有provider支持該Authentication
        if (!provider.supports(toTest)) {
            continue;
        }

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

        try {
            // 2. 真正的邏輯判斷
            result = provider.authenticate(authentication);

            if (result != null) {
                copyDetails(authentication, result);
                break;
            }
        }
        catch (AccountStatusException e) {
           ……
        }
    }

    ……
}
複製代碼

這裏首先經過 provider 判斷是否支持當前傳入進來的Authentication,目前咱們使用的是UsernamePasswordAuthenticationToken,由於除了賬號密碼登陸的方式,還會有其餘的方式,好比JwtAuthenticationToken服務器

從總體來看Authentication 的實現類如圖:session

官方 API 文檔列出了全部的子類

從總體來看AuthenticationProvider的實現類如圖:

官方 API 文檔列出了全部的子類

根據咱們目前所使用的UsernamePasswordAuthenticationToken,provider 對應的是AbstractUserDetailsAuthenticationProvider抽象類的子類DaoAuthenticationProvider,其authenticate()屬於抽象類自己的方法。

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, () -> messages.getMessage(
                            "AbstractUserDetailsAuthenticationProvider.onlySupports",
                            "Only UsernamePasswordAuthenticationToken is supported"));

    // Determine username
    String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
        : authentication.getName();

    boolean cacheWasUsed = true;
    // 1.從緩存中獲取 UserDetails
    UserDetails user = this.userCache.getUserFromCache(username);

    if (user == null) {
        cacheWasUsed = false;

        try {
            // 2.緩存獲取不到,就去接口實現類中獲取
            user = retrieveUser(username,
                                (UsernamePasswordAuthenticationToken) authentication);
        }
        catch (UsernameNotFoundException notFound) {
            ……
        }

        Assert.notNull(user,
                       "retrieveUser returned null - a violation of the interface contract");
    }

    try {
        // 3.用戶信息預檢查(用戶是否密碼過時,用戶信息被刪除等)
        preAuthenticationChecks.check(user);
        // 4.附加的檢查(密碼檢查:匹配用戶的密碼和服務器中的用戶密碼是否一致)
        additionalAuthenticationChecks(user,
                                       (UsernamePasswordAuthenticationToken) authentication);
    }
    catch (AuthenticationException exception) {
        if (cacheWasUsed) {
            // There was a problem, so try again after checking
            // we're using latest data (i.e. not from the cache)
            cacheWasUsed = false;
            user = retrieveUser(username,
                                (UsernamePasswordAuthenticationToken) authentication);
            preAuthenticationChecks.check(user);
            additionalAuthenticationChecks(user,
                                           (UsernamePasswordAuthenticationToken) authentication);
        }
        else {
            throw exception;
        }
    }

    // 5.最後的檢查
    postAuthenticationChecks.check(user);

    ……

    // 6.返回真正的通過認證的Authentication 
    return createSuccessAuthentication(principalToReturn, authentication, user);
}
複製代碼

注意: retrieveUser()的具體方法實現是由DaoAuthenticationProvider類完成的:

public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {	
	
    protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        prepareTimingAttackProtection();
        try {
            // 獲取用戶信息
            UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
            if (loadedUser == null) {
                throw new InternalAuthenticationServiceException(
                        "UserDetailsService returned null, which is an interface contract violation");
            }
            return loadedUser;
        }
        catch (UsernameNotFoundException ex) {
            ……
        }
    }
}
複製代碼

同時createSuccessAuthentication()的方法也是由DaoAuthenticationProvider類來完成的:

// 子類拿 user 對象
@Override
protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
    boolean upgradeEncoding = this.userDetailsPasswordService != null
        && this.passwordEncoder.upgradeEncoding(user.getPassword());
    if (upgradeEncoding) {
        String presentedPassword = authentication.getCredentials().toString();
        String newPassword = this.passwordEncoder.encode(presentedPassword);
        user = this.userDetailsPasswordService.updatePassword(user, newPassword);
    }
    // 調用父類的方法完成 Authentication 的建立
    return super.createSuccessAuthentication(principal, authentication, user);
}

// 建立已認證的 Authentication
protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
    UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
        principal, authentication.getCredentials(),
        authoritiesMapper.mapAuthorities(user.getAuthorities()));
    result.setDetails(authentication.getDetails());

    return result;
}
複製代碼

小結:authenticate()的認證邏輯

  1. 去調用本身實現的UserDetailsService,返回UserDetails
  2. 對 UserDetails 的信息進行校驗,主要是賬號是否被凍結,是否過時等
  3. 對密碼進行檢查,這裏調用了PasswordEncoder,檢查 UserDetails 是否可用。
  4. 返回通過認證的Authentication

編碼技巧提示:這裏在認證以前使用了Assert.isInstanceOf()進行斷言校驗,方法內部也不斷用了Assert.notNull(),這種編碼很是的靈巧,省去了後續的類型判斷。

這裏的兩次對UserDetails的檢查,主要就是經過它的四個返回 boolean 類型的方法(isAccountNonExpired()isAccountNonLocked()isCredentialsNonExpired()isEnabled())。

通過信息的校驗以後,經過UsernamePasswordAuthenticationToken的全參構造方法,返回了一個已通過認證的Authentication

拿到通過認證的Authentication以後,至此UsernamePasswordAuthenticationFilter的過濾步驟就徹底結束了,以後就會進入BasicAuthenticationFilter,具體來講就是去調用successHandler。或者未經過認證,去調用failureHandler

已認證數據共享

完成了用戶認證處理流程以後,咱們思考一下是如何在多個請求之間共享這個認證結果的呢?由於沒有作關於這方面的配置,因此能夠聯想到默認的方式應該是在session中存入了認證結果。思考:那麼是何時存放入session中的呢?

認證流程完畢以後,再看是誰調用的它,發現是AbstractAuthenticationProcessingFilterdoFilter()進行調用的,這是AbstractAuthenticationProcessingFilter繼承關係結構圖:

當認證成功以後會調用successfulAuthentication(request, response, chain, authResult),該方法中,不只調用了successHandler,還有一行比較重要的代碼:

public abstract class AbstractAuthenticationProcessingFilter extends GenericFilterBean implements ApplicationEventPublisherAware, MessageSourceAware {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // 調用了 UsernamePasswordAuthenticationFilter
        authResult = attemptAuthentication(request, response);
        
        ……
        // 調用方法,目的是保存到session 
        successfulAuthentication(request, response, chain, authResult);
    }

    // 將成功認證的用戶信息保存到session
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {

        if (logger.isDebugEnabled()) {
            logger.debug("Authentication success. Updating SecurityContextHolder to contain: "
                         + authResult);
        }

        // 保存到 SecurityContextHolder 的靜態屬性 SecurityContextHolderStrategy 裏, 很是重要的代碼
        SecurityContextHolder.getContext().setAuthentication(authResult);

        rememberMeServices.loginSuccess(request, response, authResult);

        // Fire event
        if (this.eventPublisher != null) {
            eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
                authResult, this.getClass()));
        }

        successHandler.onAuthenticationSuccess(request, response, authResult);
    }
}

// SecurityContextHolder類中存着 靜態屬性:SecurityContextHolderStrategy
public class SecurityContextHolder {
    ……
    private static SecurityContextHolderStrategy strategy;
    ……

    public static void setContext(SecurityContext context) {
        strategy.setContext(context);
    }
}
複製代碼

SecurityContextHolderStrategy接口的全部實現類:

很是顯眼的看出:ThreadLocalSecurityContextHolderStrategy類:

final class ThreadLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {

    private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<>();

    ……

    public void setContext(SecurityContext context) {
        Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
        // 將已認證的用戶對象保存到 ThreadLocal<SecurityContext> 中
        contextHolder.set(context);
    }
    ……
}
複製代碼

注意:SecurityContext類的equals()hashCode()方法已經重寫了,用來保證了authentication的惟一性。

身份認證成功後,最後在UsernamePasswordAuthenticationFilter返回後會進入一個AbstractAuthenticationProcessingFilter類中調用successfulAuthentication()方法,這個方法最後會返回咱們本身定義的登陸成功處理器handler

在返回以前,它會調用SecurityContext,最後將認證的結果放入SecurityContextHolder中,SecurityContext 類很簡單,重寫了equals() 方法和hashCode()方法,保證了authentication的惟一性。

從代碼能夠看出:SecurityContextHolder類其實是對ThreadLocal的一個封裝,能夠在不一樣方法之間進行通訊,能夠簡單理解爲線程級別的一個全局變量。

所以,能夠在同一個線程中的不一樣方法中獲取到認證信息。最後會被SecurityContextPersistenceFilter過濾器使用,這個過濾器的做用是:

當一個請求來的時候,它會將 session 中的值傳入到該線程中,當請求返回的時候,它會判斷該請求線程是否有 SecurityContext,若是有它會將其放入到 session 中,所以保證了請求結果能夠在不一樣的請求之間共享。

用戶認證流程總結

引用徐靖峯在我的博客Spring Security(一)--Architecture Overview中的歸納性總結,很是的到位:

  1. 用戶名和密碼被過濾器獲取到,封裝成Authentication,一般狀況下是UsernamePasswordAuthenticationToken這個實現類。
  2. AuthenticationManager 身份管理器負責驗證這個Authentication
  3. 認證成功後,AuthenticationManager身份管理器返回一個被填充滿了信息的(包括上面提到的權限信息,身份信息,細節信息,但密碼一般會被移除)Authentication實例。
  4. SecurityContextHolder安全上下文容器將第3步填充了信息的Authentication,經過SecurityContextHolder.getContext().setAuthentication(…)方法,設置到其中。

高度歸納起來本章節全部用的核心認證相關接口:SecurityContextHolder

身份信息的存放容器,Authentication是身份信息的抽象,AuthenticationManager是身份認證器,通常經常使用的是用戶名+密碼的身份認證器,還有其它認證器,如郵箱+密碼、手機號碼+密碼等。

再引用一張十分流行的流程圖來表示用戶的認證過程:

架構概覽圖

爲了更加形象的理解,在徐靖峯大佬的經典架構圖之上,根據本身的理解,作了更多的細化和調整:

獲取認證用戶信息

若是咱們須要獲取用的校驗過的全部信息,該如何獲取呢?上面咱們知道了會將校驗結果放入 session 中,所以,咱們能夠經過 session 獲取:

@GetMapping("/me1")
@ResponseBody
public Object getMeDetail() {
    return SecurityContextHolder.getContext().getAuthentication();
}

@GetMapping("/me2")
@ResponseBody
public Object getMeDetail(Authentication authentication){
    return authentication;
}
複製代碼

在登陸成功以後,上面有兩種方式來獲取,訪問上面的請求,就會獲取用戶所有的校驗信息,包括ip地址等信息。

若是咱們只想獲取用戶名和密碼以及它的權限,不須要ip地址等太多的信息可使用下面的方式來獲取信息:

@GetMapping("/me3")
@ResponseBody
public Object getMeDetail(@AuthenticationPrincipal UserDetails userDetails){
    return userDetails;
}
複製代碼

參考資料:

www.cnkirito.moe/spring-secu…

blog.csdn.net/u013435893/…

blog.csdn.net/qq_37142346…

我的博客:woodwhale's blog

博客園:木鯨魚的博客

相關文章
相關標籤/搜索