当前位置:网站首页>Redis - Use lua script to control the number of wrong passwords and lock the account
Redis - Use lua script to control the number of wrong passwords and lock the account
2022-08-10 23:18:00 【Technical log】
Lua script: It is a Redis scripting language with similar functions to pipelines. The client can send multiple statements to the server in batches; however, the statements sent by the Lua script are atomic, and the pipelineSent statements are not atomic.
Lua script code: Store the code in resource --> loginFailLimit.lua file;
local key = KEYS[1]local limit = tonumber(ARGV[1]) ----> the number of times to set the limitlocal limitTime = tonumber(ARGV[2]) ----> Set the time limitlocal lockTime = tonumber(ARGV[3]) ----> account lock timelocal current = tonumber(redis.call('get', key) or '0')-- Incorrect limit times per limitTime, the account will be locked for lockTime time;if(current == 0) thenredis.call('incrBy', key, "1");redis.call('expire', key, limitTime);return 0;elseif (current < limit) thenredis.call('incrBy', key, "1");return 0;elseif (current == limit) thenredis.call('incrBy', key, "1");redis.call('expire', key, lockTime);return 1;elsereturn 1;end;
Configure DefautRedisScript:
@Beanpublic DefaultRedisScript redisScript() {DefaultRedisScript objectDefaultRedisScript = new DefaultRedisScript<>();objectDefaultRedisScript.setResultType(Boolean.class);objectDefaultRedisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("loginFailLimit.lua")));return objectDefaultRedisScript;}
Create constant class:
public class RedisConstant {public final static String LIMIT = "5"; //Lock after n failures:public final static String LIMIT_TIME = "600"; //Time range: unit/secondpublic final static String LOCK_TIME = "600"; //Account lock time: unit/second}
Create RedisUtil tool class:
@Componentpublic class RedisUtil {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate DefaultRedisScript redisScript;/*** Get key lock status:* @param key* @return true - locked; false - unlocked;*/public Boolean getLockState(String key) {return (Boolean) redisTemplate.execute(redisScript,Arrays.asList(key),RedisConstant.LIMIT, RedisConstant.LIMIT_TIME, RedisConstant.LOCK_TIME);}/*** Get the remaining lock time of the key:* @param key* @return*/public long getLockInvalidTime(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** Get the value of key:* @param key* @return*/public String getValueByKey(String key) {return redisTemplate.opsForValue().get(key);}}
Test:
@SpringBootTestclass RedisLuaApplicationTests {@Autowiredprivate RedisUtil redisUtil;final String LOGIN_KEY = "PASSWORD_ERROR_KEY_";@Testvoid contextLoads() {//Simulate password error:if (true){String key = LOGIN_KEY + "userId";Boolean flag = redisUtil.getLockState(key);if (flag){long lockInvalidTime = redisUtil.getLockInvalidTime(key);System.out.println("The password is frequently incorrect and has been locked! Please log in again after "+lockInvalidTime+" seconds or contact the system administrator!");return;}String value = redisUtil.getValueByKey(key);int i = Integer.parseInt(RedisConstant.LIMIT) - Integer.parseInt(value);System.out.println("Wrong password, remaining "+i+" chance");return;}}}
边栏推荐
猜你喜欢
Detailed installation steps and environment configuration of geemap
开源一夏 | 盘点那些 Golang 标星超过 20 K 的优质项目
windows10安装PostgreSQL14避坑分享
MySQL学习笔记(1)——基础操作
DC-9靶场下载及渗透实战详细过程(DC靶场系列)
怼不过产品经理?因为你不懂DDD领域建模与架构设计
基于深度学习的三维点云分割综述
How many threads does LabVIEW allocate?
亲测有效|处理风控数据特征缺失的一种方法
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
随机推荐
JS中使用正则表达式g模式和非g模式的区别
ArcGIS中的坐标系统和投影变换
August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core
Qualcomm Platform Development Series Explanation (Application) Introduction to QCMAP Application Framework
LabVIEW分配多少线程?
带你造轮子,自定义一个随意拖拽可吸边的View
消息队列总结
MySQL学习笔记(1)——基础操作
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
leetcode:355. 设计推特
[MySQL] Using join buffer (Block Nested Loop) in left join due to character set in mysql
信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全
2021IDEA创建web工程
Flink(Pometheus监控)
高精度乘法
开源一夏 | 盘点那些 Golang 标星超过 20 K 的优质项目
"DevOps Night Talk" - Pilot - Introduction to CNCF Open Source DevOps Project DevStream - feat. PMC member Hu Tao
基于深度学习的三维点云分割综述
OneNote 教程,如何在 OneNote 中整理笔记本?
Glide监听Activity生命周期源码分析