DaoAuthenticationProvider
DaoAuthenticationProvider
是一個 AuthenticationProvider
實現,它使用 UserDetailsService
和 PasswordEncoder
來認證使用者名稱和密碼。
本節將探討 DaoAuthenticationProvider
在 Spring Security 中如何工作。下圖解釋了 讀取使用者名稱和密碼 部分圖中 AuthenticationManager
的工作原理。

圖 1.
DaoAuthenticationProvider
用法 來自 讀取使用者名稱和密碼 部分的認證
Filter
將一個 UsernamePasswordAuthenticationToken
傳遞給 AuthenticationManager
,該管理器由 ProviderManager
實現。
ProviderManager
被配置為使用型別為 DaoAuthenticationProvider
的 AuthenticationProvider。
DaoAuthenticationProvider
從 UserDetailsService
中查詢 UserDetails
。
DaoAuthenticationProvider
使用 PasswordEncoder
來驗證上一步返回的 UserDetails
中的密碼。
當認證成功時,返回的
Authentication
型別為 UsernamePasswordAuthenticationToken
,其主體是配置的 UserDetailsService
返回的 UserDetails
。最終,返回的 UsernamePasswordAuthenticationToken
由認證 Filter
設定到 SecurityContextHolder
中。