栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

若依RuoYi-Vue代码学习一---【DEBUG】登录验证时,Spring Security怎么帮我们查的用户信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

若依RuoYi-Vue代码学习一---【DEBUG】登录验证时,Spring Security怎么帮我们查的用户信息

一、登录验证时,Spring Security怎么帮我们查的用户信息
  • 之前说个SysLoginService有登录验证的方法
    public String login(String username, String password, String code, String uuid)
    {
        boolean captchaonOff = configService.selectCaptchaOnOff();
        // 验证码开关
        if (captchaOnOff)
        {   //校验验证码
            validateCaptcha(username, code, uuid);
        }
        // 用户验证
        Authentication authentication = null;
        try
        {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
            //UsernamePasswordAuthenticationToken [Principal=com.ruoyi.common.core.domain.model.LoginUser@748cc4df, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]

        }
        catch (Exception e)
        {
            if (e instanceof BadCredentialsException)
            {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
                throw new UserPasswordNotMatchException();
            }
            else
            {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
                throw new ServiceException(e.getMessage());
            }
        }
        //记录登录信息=====新增系统登录日志
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();//com.ruoyi.common.core.domain.model.LoginUser@748cc4df

        //记录登录信息=====修改用户基本信息(最后IP、时间)
        recordLoginInfo(loginUser.getUserId());
        // 生成token
        return tokenService.createToken(loginUser);
    }

今天主要说的是其中的
authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(username, password));
这一段代码怎么(也就是spring security怎么帮我们查的用户名、密码及用户信息)的【执行流程】===通过DEBUG来仔细看看

二、开始
  • 第一次进入

  • 走到后面的其中一个方法determineUsername确定用户名,方法位于【抽象用户详细信息身份验证提供者org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider】

private String determineUsername(Authentication authentication) {
		return (authentication.getPrincipal() == null) ? "NONE_PROVIDED" : authentication.getName();
	}
  • 可以看到这个方法是三目,如果authentication.getPrincipal() == null【用户名不是空】,则getName()

  • 上面这个方法在哪里使用呢

  • 检索用户方法retrieveUser中

  • 之后走到若依自己的用户服务com.ruoyi.framework.web.service.UserDetailsServiceImpl

()!!!到这里就没什么好说的了,懂得都懂。

  • 接着user变量就有了数据

  • 也可以看看上面,我们之前说的先查username,和没有走缓存

  • 在查询结束后,还会有一些认证preAuthenticationChecks、preAuthenticationChecks、additionalAuthenticationChecks

  • 最后返回【创建成功认证】createSuccessAuthentication

  • createSuccessAuthentication方法内部

  • 创建认证

    代码中的官方注解

// 确保我们返回用户提供的原始凭据, 
// 因此即使使用编码密码,后续尝试也能成功。 
// 还要确保我们返回原始的 getDetails(),以便将来缓存到期后的 
// 身份验证事件包含详细信息
三、最后说下一个常用且常见的方法getPrincipal()

位于org.springframework.security.authentication.UsernamePasswordAuthenticationToken

  • 这就是getPrincipal()的数据了,就是拿user的

  • 然后,挂一张依赖图看看

那么,就到这里了。(^o^)/~!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592022.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号