账号登录,设置密码锁(一小时内错误3次账号锁定无法登录)

本文介绍了如何在Spring Boot应用中实现用户登录功能,使用Controller层接收请求并调用UserService进行密码验证。特别关注了如何利用Redis实现用户锁定及计时功能,确保账号安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里登录我是从UserController中摘出来的,为了设置拦截器用的

controller层

  /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/toLogin")
    public ResultBody<MenuPo> queryUserById(@RequestBody User user) throws JsonProcessingException {
        ResultBody login = userService.queryNameByMenu(user);
        return login;
    }

UserService层

 /**
    * 登录
    * @param user
    * @return
    */
   ResultBody queryNameByMenu(User user) throws JsonProcessingException;

UserServiceImpl层 这里测试我用的是60s


private static final String userName ="userName:";
    public static final Long time = 60L;
/**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public ResultBody queryNameByMenu(User user) throws JsonProcessingException {

        //判断是否锁定
        String b = combinationLock(user.getName());
        if(b != null){
            return ResultBody.error(CommonEnum.TIME_OUT.getResultCode(), CommonEnum.TIME_OUT.getResultMsg(),b);
        }

        //通过用户名查询
        User name = userMapper.selectOne(new QueryWrapper<User>()
                .eq("name", user.getName()));
        if (name == null) {

            if(b != null){
                return ResultBody.error(CommonEnum.TIME_OUT.getResultCode(), CommonEnum.TIME_OUT.getResultMsg(),b);
            }
            return ResultBody.error(CommonEnum.NAME_ERROR.getResultCode(), CommonEnum.NAME_ERROR.getResultMsg());

        } else {
            //通过用户比对密码
            if (name.getPassword().equals(user.getPassword())) {
                MenuPo menuPo = userMapper.queryNameByMenu(user);
                //token
                String sign = JwtUtils.sign(user.getName());
                menuPo.setToken(sign);
                //httpServletRequest.getSession().setAttribute(user.getName(),name);
                redisUtils.redisStringSet(sign,user.getName());
                return ResultBody.success(menuPo);
            } else {

                if(b != null){
                    return ResultBody.error(CommonEnum.TIME_OUT.getResultCode(), CommonEnum.TIME_OUT.getResultMsg(),b);
                }
                return ResultBody.error(CommonEnum.NAME_ERROR.getResultCode(), CommonEnum.NAME_ERROR.getResultMsg());

            }

        }

    }

  /**
     * 密码锁
     * @return
     */
    public String combinationLock(String name) throws JsonProcessingException {
        String s = redisUtils.redisStringGet(userName + name);
        if(s==null){
            CombinationLockPo combinationLockPo = new CombinationLockPo();
            combinationLockPo.setNum(1);
            redisUtils.redisStringSet(userName + name,combinationLockPo,time);

        }else {
            CombinationLockPo parse = JSONObject.parseObject(s,CombinationLockPo.class);
            //判断是否有无超时
            if(parse.getTime() == null){
                //判断登录次数三次
                if(parse.getNum() < 3){
                    parse.setNum(parse.getNum()+1);
                    redisUtils.redisStringSet(userName + name,parse,time);
                }else {
                    Long oneHour = Long.valueOf(1000*60);  //60s  用于测试
                    long time = new Date().getTime();
                    Long overTime = oneHour + time;
                    parse.setTime(overTime);
                    redisUtils.redisStringSet(userName + name,parse,time);

                }
            }else {
                long newTime = new Date().getTime();
                Long time = parse.getTime();
                if(time > newTime){
                    Long lo = time - newTime;
                    Long lon = lo/1000;
                    return lon.toString();
                }
                else {
                    redisUtils.redisDelteKey(userName + name);
                }
            }

        }
        return null;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alu:

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值