• spring-security查询数据库源码解析


    版本是2.1.0.RELEASE;

    创建UsernamePasswordAuthenticationToken

    private final AuthenticationManagerBuilder authenticationManagerBuilder;
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
    

    参数解析:
    username和password是前端传过来的

    点进去authenticate()方法,有多个实现类,选择ProviderManager

    image
    image

    继续点进去authenticate()方法,有多个实现类,选择AbstractUserDetailsAuthenticationProvider

    image
    首先是有一个判断,从Cache中取,实现类走的是NullUserCache;如果返回null,调用receiveUser方法;如果不是返回null,则检查user是否正确;
    image
    image
    我们这里返回null,所以走的是receiveUser,点进去;
    image

    点进去loadUserByUsername()方法

    找到我们自己的实现类UserDetailsServiceImpl,实现了UserDetailsService该接口;
    image
    这个方法是我们自己重写的;
    image
    先判断是否配置登录缓存,有缓存的话,直接get就行;
    image
    如果没有缓存,会先查询数据库,然后再次放入缓存,方便下次直接取出;
    image

    跳出loadUserByUsername,在跳出receiveUser,继续往下走

    image
    调用的是该实现类AbstractUserDetailsAuthenticationProvider的内部类DefaultPreAuthenticationChecks的check方法;该check方法检查的就是用户是否锁定,是否被禁用,是否超时;
    image
    进去additionalAuthenticationChecks方法,对比密码是否匹配
    就是检查前端传过来的password和通过username查出来的password是否一样,不一样则抛出异常;
    image
    再检查凭证是否超时
    image
    image
    最后就是依据正确的信息创建Authentication
    image

  • 相关阅读:
    CMake基本语法
    Immutable使用与react+redux
    mysql 安装之docker
    elasticsearch 安装之docker 单机
    局域网共享时提示:你没有权限访问,请与网络管理员联系
    【python、Neo4j】 python3.6环境安装neo4j客户端
    【python】 Anaconda jupyter 服务正常,内核启动失败, ipython启动失败
    Neo4j 手动安装apoc插件
    Ubuntu16 安装配置Neo4j
    node工具之nodemon
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/14771242.html
Copyright © 2020-2023  润新知