SpringSecurity 用户名和密码的校验过程及自定义密码验证

1,028 阅读1分钟
  1. 先将用于登录的账号密码存到UsernamePasswordAuthenticationToken

  2. 执行 AuthenticationManager 认证方法authenticate(UsernamePasswordAuthenticationToken)

  3. ProviderManager实现了AuthenticationManager.authenticate(UsernamePasswordAuthenticationToken)

  4. ProviderManager 是通过自身管理的n个AuthenticationProvider认证提供者去进行认证

  5. AuthenticationProvider认证提供者 使用自身的authenticate(Authentication)方法;

  6. AuthenticationProviderauthenticate(Authentication)方法是被AbstractUserDetailsAuthenticationProvider所实现

  7. AbstractUserDetailsAuthenticationProvider 抽象用户细节认证提供者 会调用 自身声明的retrieveUser抽象方法来检索用户

  8. retrieveUser抽象方法在DaoAuthenticationProvider 持久层认证提供者 中进行了体现

  9. DaoAuthenticationProvider 持久层认证提供者 包含 UserDetailsService 用户细节处理器,

  10. 用户细节处理器的loadUserByUsername方法又被自定义的UserDetailsServiceImpl所实现

  11. UserDetailsServiceImpl实现类取出数据库中的该登录名的数据(selectUserByUserName),并将用户的菜单权限数据和基本信息封装成一个UserDetails用户细节返回!

  12. AbstractUserDetailsAuthenticationProvider 抽象用户细节认证提供者 最终获取到UserDeatils

  13. 然后AbstractUserDetailsAuthenticationProvider 调用additionalAuthenticationChecks方法对用户的密码进行最后的检查

  14. 密码的校验是由BCryptPasswordEncoder 通过实现PasswordEncoder 的matches方法来完成,

  15. BCryptPasswordEncoder.matches 方法会校验密文是否属于自己的编码格式,最终密码校验的细节完全在BCrypt实体类中进行