DaoAuthenticationProvider

DaoAuthenticationProvider 是一個 AuthenticationProvider 實作,它使用 UserDetailsServicePasswordEncoder 來驗證使用者名稱和密碼。

本節將檢視 DaoAuthenticationProvider 在 Spring Security 中是如何運作的。下圖解釋了 AuthenticationManager 的運作方式,圖例來自讀取使用者名稱 & 密碼 章節。

daoauthenticationprovider
圖 1. DaoAuthenticationProvider 用法

number 1 來自讀取使用者名稱 & 密碼 章節的驗證 FilterUsernamePasswordAuthenticationToken 傳遞給由 ProviderManager 實作的 AuthenticationManager

number 2 ProviderManager 設定為使用類型為 DaoAuthenticationProviderAuthenticationProvider

number 3 DaoAuthenticationProviderUserDetailsService 查找 UserDetails

number 4 DaoAuthenticationProvider 使用 PasswordEncoder 來驗證上一步驟傳回的 UserDetails 上的密碼。

number 5 當驗證成功時,傳回的 Authentication 類型為 UsernamePasswordAuthenticationToken,且其 principal 是由設定的 UserDetailsService 傳回的 UserDetails。最終,傳回的 UsernamePasswordAuthenticationToken 由驗證 Filter 設定在 SecurityContextHolder 上。