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

当用户登录验证码错误次数太多时,需要限制用户在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) + "分钟后登录!");
        }
    }
相关推荐
PineappleCoder6 小时前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder6 小时前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
45288655上山打老虎6 小时前
C++完美转发
java·jvm·c++
Seven976 小时前
查找算法
java
Mr.Pascal6 小时前
Redis:主动更新,读时更新,定时任务。三种的优劣势对比
数据库·redis·缓存
m0_471199636 小时前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
GIS之路7 小时前
GDAL 读取KML数据
前端
今天不要写bug7 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
毕设源码-朱学姐7 小时前
【开题答辩全过程】以 公务员考试在线测试系统为例,包含答辩的问题和答案
java