用户登录错误次数太多锁定账号

当用户登录验证码错误次数太多时,需要限制用户在10分钟之内不能再次登录。

限制方案:

1.通过Redis ZSet

key可以设置为用户名,value可以设置为UUID,score设置为当前时间戳

每次用户登录时,通过 rangeByScore 查询对应的限制时间范围内错误的的次数,如果次数超过阈值,则限制登录。

java 复制代码
private void limitErrorCount(String mobile) {
        long currentTimeMillis = System.currentTimeMillis();
        if(redisTemplate.hasKey(LOGIN_VC_ERROR_COUNT_KEY + mobile)) {
            Integer size = redisTemplate.opsForZSet().rangeByScore(LOGIN_VC_ERROR_COUNT_KEY + mobile, currentTimeMillis - 3 * 60 * 1000, currentTimeMillis).size();
            if (size != null && size >= 4) {
                redisTemplate.opsForValue().set(LOGIN_VC_ACCOUNT_LOCK_KEY + mobile, true, 10, TimeUnit.MINUTES);
                throw new BusinessException("验证码输入错误,账号将被锁定10分钟!");
            }
        }
        redisTemplate.opsForZSet().add(LOGIN_VC_ERROR_COUNT_KEY + mobile, UUID.randomUUID().toString(), currentTimeMillis);
        redisTemplate.expire(LOGIN_VC_ERROR_COUNT_KEY + mobile, 4, TimeUnit.MINUTES);
    }
java 复制代码
private void checkAccountLock(String mobile) {
        Boolean accountLock = (Boolean)redisTemplate.opsForValue().get(LOGIN_VC_ACCOUNT_LOCK_KEY + mobile);
        if(accountLock != null && BooleanUtils.isTrue(accountLock)) {
            Long expireTime = redisTemplate.opsForValue().getOperations().getExpire(LOGIN_VC_ACCOUNT_LOCK_KEY + mobile, TimeUnit.MINUTES);
            throw new BusinessException("账号已被锁定,请在" + (expireTime == null ? 1 : expireTime) + "分钟后登录!");
        }
    }
相关推荐
arvin_xiaoting4 小时前
OpenClaw学习总结_I_核心架构_8:SessionPruning详解
前端·chrome·学习·系统架构·ai agent·openclaw·sessionpruning
工程师老罗5 小时前
Image(图像)的用法
java·前端·javascript
leo_messi945 小时前
2026版商城项目(一)
java·elasticsearch·k8s·springcloud
美味蛋炒饭.6 小时前
Tomcat 超详细入门教程(安装 + 目录 + 配置 + 部署 + 排错)
java·tomcat
鹿角片ljp6 小时前
苍穹外卖 day05:店铺营业状态设置与Redis入门实战
数据库·redis·缓存
dreamxian6 小时前
苍穹外卖day11
java·spring boot·后端·spring·mybatis
Veggie266 小时前
【Java深度学习】PyTorch On Java 系列课程 第八章 17 :模型评估【AI Infra 3.0】[PyTorch Java 硕士研一课程]
java·人工智能·深度学习
weisian1516 小时前
Java并发编程--19-ThreadPoolExecutor七参数详解:拒绝Executors,手动掌控线程池
java·线程池·threadpool·七大参数
csdn5659738506 小时前
Java打包时,本地仓库有jar 包,Maven打包却还去远程拉取
java·maven·jar
swipe6 小时前
把 JavaScript 原型讲透:从 `[[Prototype]]`、`prototype` 到 `constructor` 的完整心智模型
前端·javascript·面试