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

当用户登录验证码错误次数太多时,需要限制用户在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) + "分钟后登录!");
        }
    }
相关推荐
小yu学编程2 分钟前
IDEA 2025版本中如何设置包层级结构
java·ide·intellij-idea·层级结构
YXWik62 分钟前
CodeGraph安装及在idea的claude code插件中使用
java·ide·intellij-idea
zzipeng4 分钟前
Linux 并发与竞争
java·linux·运维
276695829211 分钟前
京东随机变速滑块拼图验证码识别(京东E卡)
java·服务器·前端·python·京东滑块·京东变速滑块·京东e卡绑卡
未若君雅裁17 分钟前
ArrayList 源码全解析:动态扩容、数组互转与底层原理
java
Java程序员-小白25 分钟前
Spring Boot整合Sa-Token框架(入门篇)
java·spring boot·后端·sa-token
এ慕ོ冬℘゜25 分钟前
手写生产级 jQuery Toast 轻量提示组件|零插件依赖、动画流畅、极简高
前端·javascript·jquery
NE_STOP30 分钟前
Docker--初识Dockerfile
java
Oneslide1 小时前
UI设计-企业OA风格
前端