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 中。