当前位置:   article > 正文

Spring-security-oauth2之DaoAuthenticationProvider

public daoauthenticationprovider authenticationprovider()

    Spring-security-oauth2的版本是2.3.5.RELEASE

    Spring-security的版本是5.1.4.RELEASE

比较登录的用户的密码是否与数据库中对应的密码一致

    List-1

  1. public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
  2. private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";
  3. private PasswordEncoder passwordEncoder;
  4. private volatile String userNotFoundEncodedPassword;
  5. private UserDetailsService userDetailsService;
  6. private UserDetailsPasswordService userDetailsPasswordService;
  7. public DaoAuthenticationProvider() {
  8. this.setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
  9. }
  10. protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
  11. if (authentication.getCredentials() == null) {
  12. this.logger.debug("Authentication failed: no credentials provided");
  13. throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
  14. } else {
  15. String presentedPassword = authentication.getCredentials().toString();
  16. if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
  17. this.logger.debug("Authentication failed: password does not match stored value");
  18. throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
  19. }
  20. }
  21. }

    如List-1中所示,重点是"if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {"这行代码,这行中presentedPassword表示客户端提交的密码,而userDetails.getPassword()则是从数据库中取出的密码,判断是否一样,不一样则说明密码错误。

    我们来看父类AbstractUserDetailsAuthenticationProvider中的authenticate方法,如下List-2,注意List-2中的"this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);",它调用的是DaoAuthenticationProvider的additionalAuthenticationChecks方法,见上面的List-1。

    List-2

  1. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  2. Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, () -> {
  3. return this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.onlySupports", "Only UsernamePasswordAuthenticationToken is supported");
  4. });
  5. String username = authentication.getPrincipal() == null ? "NONE_PROVIDED" : authentication.getName();
  6. boolean cacheWasUsed = true;
  7. UserDetails user = this.userCache.getUserFromCache(username);
  8. if (user == null) {
  9. cacheWasUsed = false;
  10. try {
  11. user = this.retrieveUser(username, (UsernamePasswordAuthenticationToken)authentication);
  12. } catch (UsernameNotFoundException var6) {
  13. this.logger.debug("User '" + username + "' not found");
  14. if (this.hideUserNotFoundExceptions) {
  15. throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
  16. }
  17. throw var6;
  18. }
  19. Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract");
  20. }
  21. try {
  22. this.preAuthenticationChecks.check(user);
  23. this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);
  24. } catch (AuthenticationException var7) {
  25. if (!cacheWasUsed) {
  26. throw var7;
  27. }
  28. cacheWasUsed = false;
  29. user = this.retrieveUser(username, (UsernamePasswordAuthenticationToken)authentication);
  30. this.preAuthenticationChecks.check(user);
  31. this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);
  32. }
  33. this.postAuthenticationChecks.check(user);
  34. if (!cacheWasUsed) {
  35. this.userCache.putUserInCache(user);
  36. }
  37. Object principalToReturn = user;
  38. if (this.forcePrincipalAsString) {
  39. principalToReturn = user.getUsername();
  40. }
  41. return this.createSuccessAuthentication(principalToReturn, authentication, user);
  42. }

    List-2中的"return this.createSuccessAuthentication(principalToReturn, authentication, user);"调用的是DaoAuthenticationProvider的createSuccessAuthentication方法。    

 

Reference

  1. Spring-security-oauth2源码

转载于:https://my.oschina.net/u/2518341/blog/3022238

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/534046
推荐阅读
相关标签
  

闽ICP备14008679号