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

DaoAuthenticationProvider
用法 來自讀取使用者名稱 & 密碼 章節的驗證
Filter
將 UsernamePasswordAuthenticationToken
傳遞給由 ProviderManager
實作的 AuthenticationManager
。
ProviderManager
設定為使用類型為 DaoAuthenticationProvider
的 AuthenticationProvider。
DaoAuthenticationProvider
從 UserDetailsService
查找 UserDetails
。
DaoAuthenticationProvider
使用 PasswordEncoder
來驗證上一步驟傳回的 UserDetails
上的密碼。
當驗證成功時,傳回的
Authentication
類型為 UsernamePasswordAuthenticationToken
,且其 principal 是由設定的 UserDetailsService
傳回的 UserDetails
。最終,傳回的 UsernamePasswordAuthenticationToken
由驗證 Filter
設定在 SecurityContextHolder
上。