当前位置:   article > 正文

SpringSecurity用户密码验证过程_spring security 怎么验证用户名密码的

spring security 怎么验证用户名密码的

SpringSecurity过滤链当中的UsernamePasswordAuthenticationFilter负责登陆密码验证。

  • AbstractAuthenticationProcessingFilterUsernamePasswordAuthenticationFilter的父接口,接口当中就有一个this.getAuthenticationManager()方法,获取AuthenticationManager对象。
  • AuthenticationManager对象当中有一个方法:Authentication authenticate(Authentication authentication) ,传入authentication对象用来密码验证。
  • AuthenticationManager的子类ProviderManager实现了该方法,可能有多种验证方式,因此遍历一个集合,有一个满足就可以,代码如下
  1. @Override
  2. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  3. Class<? extends Authentication> toTest = authentication.getClass();
  4. // 判断使用哪种验证方式
  5. if (!provider.supports(toTest)) {
  6. continue;
  7. }
  8. ...
  9. try {
  10. // 进行真正认证的地方
  11. result = provider.authenticate(authentication);
  12. if (result != null) {
  13. copyDetails(authentication, result);
  14. break;
  15. }
  16. }
  17. return result;
  18. }
  • provider.authenticate(authentication); 其中providerAuthenticationProvider的一个子类的实现类,如果用的是用户名密码,就是AbstractUserDetailsAuthenticationProvider抽象类,该抽象类当中有一子类,DaoAuthenticationProvider,真正负责的密码验证。
  • AbstractUserDetailsAuthenticationProvider中的public Authentication authenticate(Authentication authentication)方法吗,调用
  1. protected void additionalAuthenticationChecks(UserDetails userDetails,
  2. UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
  3. if (authentication.getCredentials() == null) {
  4. this.logger.debug("Failed to authenticate since no credentials provided");
  5. throw new BadCredentialsException(this.messages
  6. .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
  7. }
  8. //presentedPassword 用户登陆信息
  9. //userDetails.getPassword()数据库根据用户名的得到的信息,判断是否相同
  10. String presentedPassword = authentication.getCredentials().toString();
  11. if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
  12. this.logger.debug("Failed to authenticate since password does not match stored value");
  13. throw new BadCredentialsException(this.messages
  14. .getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
  15. }
  16. }
  • passwordEncoder.matches 把presentedPassword加密之后与原来的结果进行比较,返回结果

其中数据库加载用户信息是从spring缓存当中进行加载,如果缓存当中没有,再从数据库加载。

参考文章
Spring Security到底在哪里进行密码方式认证
SpringSecurity是如何实现账号密码的验证登录的

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号