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

当用户登录验证码错误次数太多时,需要限制用户在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) + "分钟后登录!");
        }
    }
相关推荐
Clank的游戏栈几秒前
Unity3D 场景树与组件化开发详解
前端
ToBeWhatYouWannaBe.2 分钟前
代码随想录-Day49
java·数据结构·算法·leetcode
前端fighter5 分钟前
表单代码示例
前端·javascript·vue.js
繁星日月6 分钟前
利用docker搭建漏洞环境,使用SSRF+Redis写入centos以及ubuntu的公钥,实现免密登录
redis·安全·ubuntu·docker·容器·centos·渗透
☀️20 分钟前
Redis 的过期策略
数据库·redis·缓存
续亮~21 分钟前
9、Redis 高级数据结构 HyperLogLog 和事务
数据结构·数据库·redis
二十雨辰24 分钟前
[JS]面向对象ES6
前端·javascript·ajax
GDAL25 分钟前
css之transform-origin
前端·css
Al_WAYS77827 分钟前
redis 一 认识redis
数据库·redis·运维开发
疯狂创作者28 分钟前
十款绚丽的前端 CSS 菜单导航动画
前端·css