博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-security-oauth2之DaoAuthenticationProvider
阅读量:6408 次
发布时间:2019-06-23

本文共 4304 字,大约阅读时间需要 14 分钟。

hot3.png

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

    Spring-security的版本是5.1.4.RELEASE

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

    List-1

public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {    private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";    private PasswordEncoder passwordEncoder;    private volatile String userNotFoundEncodedPassword;    private UserDetailsService userDetailsService;    private UserDetailsPasswordService userDetailsPasswordService;    public DaoAuthenticationProvider() {        this.setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());    }    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {        if (authentication.getCredentials() == null) {            this.logger.debug("Authentication failed: no credentials provided");            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));        } else {            String presentedPassword = authentication.getCredentials().toString();            if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {                this.logger.debug("Authentication failed: password does not match stored value");                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));            }        }    }

    如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

public Authentication authenticate(Authentication authentication) throws AuthenticationException {    Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication, () -> {        return this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.onlySupports", "Only UsernamePasswordAuthenticationToken is supported");    });    String username = authentication.getPrincipal() == null ? "NONE_PROVIDED" : authentication.getName();    boolean cacheWasUsed = true;    UserDetails user = this.userCache.getUserFromCache(username);    if (user == null) {        cacheWasUsed = false;        try {            user = this.retrieveUser(username, (UsernamePasswordAuthenticationToken)authentication);        } catch (UsernameNotFoundException var6) {            this.logger.debug("User '" + username + "' not found");            if (this.hideUserNotFoundExceptions) {                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));            }            throw var6;        }        Assert.notNull(user, "retrieveUser returned null - a violation of the interface contract");    }    try {        this.preAuthenticationChecks.check(user);        this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);    } catch (AuthenticationException var7) {        if (!cacheWasUsed) {            throw var7;        }        cacheWasUsed = false;        user = this.retrieveUser(username, (UsernamePasswordAuthenticationToken)authentication);        this.preAuthenticationChecks.check(user);        this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);    }    this.postAuthenticationChecks.check(user);    if (!cacheWasUsed) {        this.userCache.putUserInCache(user);    }    Object principalToReturn = user;    if (this.forcePrincipalAsString) {        principalToReturn = user.getUsername();    }    return this.createSuccessAuthentication(principalToReturn, authentication, user);}

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

 

Reference

  1. Spring-security-oauth2源码

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

你可能感兴趣的文章
Storm集群安装详解
查看>>
Maven搭建Spring+Struts2+Hibernate项目详解
查看>>
《为什么你总是害怕来不及》
查看>>
【python】词法语法解析模块ply
查看>>
linux cat 文件操作
查看>>
caffe源码解析一 —— caffe的安装(CPU-Windows)
查看>>
【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)
查看>>
随手记统一监控平台Focus设计解析
查看>>
准备好了?测试人员迟早会被要求测试包含区块链技术的解决方案
查看>>
ODE网络:一场颠覆RNN的革命即将到来
查看>>
OKR 如何彻底激发员工积极性,挑战不可能?
查看>>
Talk is cheap, show me the architecture
查看>>
回到网易后开源APM技术选型与实战
查看>>
Unity开发日记:Animator组件详解
查看>>
MySQL主主复制+LVS+Keepalived实现MySQL高可用性
查看>>
Elasticsearch 6.6.1 发布,分布式搜索和数据分析引擎
查看>>
当我们谈网络时,我们谈些什么(1)--我们如何接入因特网
查看>>
ImageDT王芹:从场景出发,在市场验证下找到技术与零售的结合点 ...
查看>>
最新一期Spring Boot 面试题
查看>>
ovirt 使用glusterfs存储域
查看>>