DaoAuthenticationProvider

DaoAuthenticationProvider 是一个 AuthenticationProvider 实现,它使用 UserDetailsServicePasswordEncoder 来认证用户名和密码。

本节将探讨 DaoAuthenticationProvider 在 Spring Security 中如何工作。下图解释了 读取用户名和密码 部分图中 AuthenticationManager 的工作原理。

daoauthenticationprovider
图 1. DaoAuthenticationProvider 用法

number 1 来自 读取用户名和密码 部分的认证 Filter 将一个 UsernamePasswordAuthenticationToken 传递给 AuthenticationManager,该管理器由 ProviderManager 实现。

number 2 ProviderManager 被配置为使用类型为 DaoAuthenticationProviderAuthenticationProvider

number 3 DaoAuthenticationProviderUserDetailsService 中查找 UserDetails

number 4 DaoAuthenticationProvider 使用 PasswordEncoder 来验证上一步返回的 UserDetails 中的密码。

number 5 当认证成功时,返回的 Authentication 类型为 UsernamePasswordAuthenticationToken,其主体是配置的 UserDetailsService 返回的 UserDetails。最终,返回的 UsernamePasswordAuthenticationToken 由认证 Filter 设置到 SecurityContextHolder 中。