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,其主體是配置的 UserDetailsService 返回的 UserDetails,並且許可權集至少包含 FACTOR_PASSWORD。最終,返回的 UsernamePasswordAuthenticationToken 由認證 Filter 設定到 SecurityContextHolder 中。