若依框架:登陆(后端)

1、校验验证码

2、校验用户名和密码

3、生成令牌

    /**

    * 登录方法

    *

    * @param loginBody 登录信息

    * @return 结果

    */

    @PostMapping("/login")

    public AjaxResult login(@RequestBody LoginBody loginBody)

    {

        AjaxResult ajax = AjaxResult.success();

        // 生成令牌

        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),

                loginBody.getUuid());

        ajax.put(Constants.TOKEN, token);

        return ajax;

    }

    /**

    * 登录验证

    *

    * @param username 用户名

    * @param password 密码

    * @param code 验证码

    * @param uuid 唯一标识

    * @return 结果

    */

    public String login(String username, String password, String code, String uuid)

    {

        // 验证码校验

        validateCaptcha(username, code, uuid);

        // 登录前置校验

        loginPreCheck(username, password);

        // 用户验证

        Authentication authentication = null;

        try

        {

            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);

            AuthenticationContextHolder.setContext(authenticationToken);

            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername

            authentication = authenticationManager.authenticate(authenticationToken);

        }

        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());

            }

        }

        finally

        {

            AuthenticationContextHolder.clearContext();

        }

        // 记录操作日志、对应sys_logininfor表

        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        // 记录用户最近的操作信息、修改用户表、对应sys_user表

        recordLoginInfo(loginUser.getUserId());

        // 生成token

        return tokenService.createToken(loginUser);

    }

    /**

    * 校验验证码

    *

    * @param username 用户名

    * @param code 验证码

    * @param uuid 唯一标识

    * @return 结果

    */

    public void validateCaptcha(String username, String code, String uuid)

    {

        boolean captchaEnabled = configService.selectCaptchaEnabled();

        if (captchaEnabled)

        {

            String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");

            String captcha = redisCache.getCacheObject(verifyKey);

            redisCache.deleteObject(verifyKey);

            // 验证码为空说明超时、则抛出超时异常、同时用异步方式记录日志

            if (captcha == null)

            {

                // 异步任务管理器、异步记录日志、提供一个线程池、创建一个记录日志的任务交给线程池完成、和业务逻辑实现异步的分离、效率更高

                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));

                throw new CaptchaExpireException();

            }

            // 验证码计算错误、抛出异常信息

            if (!code.equalsIgnoreCase(captcha))

            {

                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));

                throw new CaptchaException();

            }

        }

    }

    /**

    * 记录登录信息

    *

    * @param userId 用户ID

    */

    public void recordLoginInfo(Long userId)

    {

        SysUser sysUser = new SysUser();

        sysUser.setUserId(userId);

        sysUser.setLoginIp(IpUtils.getIpAddr());

        sysUser.setLoginDate(DateUtils.getNowDate());

        userService.updateUserProfile(sysUser);

    }

<update id="updateUser" parameterType="SysUser">

update sys_user

<set>

<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>

<if test="userName != null and userName != ''">user_name = #{userName},</if>

<if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>

<if test="email != null ">email = #{email},</if>

<if test="phonenumber != null ">phonenumber = #{phonenumber},</if>

<if test="sex != null and sex != ''">sex = #{sex},</if>

<if test="avatar != null and avatar != ''">avatar = #{avatar},</if>

<if test="password != null and password != ''">password = #{password},</if>

<if test="status != null and status != ''">status = #{status},</if>

<if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>

<if test="loginDate != null">login_date = #{loginDate},</if>

<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>

<if test="remark != null">remark = #{remark},</if>

update_time = sysdate()

</set>

where user_id = #{userId}

</update>

    /**

    * 创建令牌

    *

    * @param loginUser 用户信息

    * @return 令牌

    */

    public String createToken(LoginUser loginUser)

    {

        String token = IdUtils.fastUUID();

        loginUser.setToken(token);

        setUserAgent(loginUser);

        refreshToken(loginUser);

        Map<String, Object> claims = new HashMap<>();

        claims.put(Constants.LOGIN_USER_KEY, token);

        return createToken(claims);

    }

    /**

    * 刷新令牌有效期

    *

    * @param loginUser 登录信息

    */

    public void refreshToken(LoginUser loginUser)

    {

        loginUser.setLoginTime(System.currentTimeMillis());

        loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);

        // 根据uuid将loginUser缓存

        String userKey = getTokenKey(loginUser.getToken());

        redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);

    }

相关推荐

  1. 框架登陆

    2024-04-03 07:20:02       15 阅读
  2. 框架学习

    2024-04-03 07:20:02       66 阅读
  3. 7.仿系统业务实践

    2024-04-03 07:20:02       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-03 07:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-03 07:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-03 07:20:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-03 07:20:02       20 阅读

热门阅读

  1. 494. 目标和(力扣LeetCode)

    2024-04-03 07:20:02       15 阅读
  2. wpf Ellipse

    2024-04-03 07:20:02       13 阅读
  3. 小参林八股

    2024-04-03 07:20:02       10 阅读
  4. notepad++ 快捷键使用

    2024-04-03 07:20:02       15 阅读
  5. vim脚本的语言语法

    2024-04-03 07:20:02       11 阅读
  6. 微前端qiankun接入Vue和React项目

    2024-04-03 07:20:02       11 阅读
  7. JVM将虚拟机分成了哪几块区域?

    2024-04-03 07:20:02       14 阅读
  8. nginx报错相关问题

    2024-04-03 07:20:02       15 阅读
  9. UDP协议

    UDP协议

    2024-04-03 07:20:02      12 阅读
  10. 深入探索Linux的lsof命令

    2024-04-03 07:20:02       14 阅读