当前位置:网站首页>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;}}}
边栏推荐
- Merge k sorted linked lists
- How to be a Righteous Hacker?What should you study?
- 实例053:按位异或
- 实例052:按位或
- 【MySQL】mysql因为字符集导致left join出现Using join buffer (Block Nested Loop)
- Mysql之部分表主从搭建及新增表
- BM13 determines whether a linked list is a palindrome
- ArcGIS中的坐标系统和投影变换
- SurfaceView 的双缓冲
- LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
猜你喜欢
ArcGIS中的坐标系统和投影变换
How to be a Righteous Hacker?What should you study?
OneNote 教程,如何在 OneNote 中整理笔记本?
DC-7靶场下载及渗透实战详细过程(DC靶场系列)
3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)
信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全
How does the Weiluntong touch screen display the current value of abnormal data while alarming?
实例055:按位取反
Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
windows10安装PostgreSQL14避坑分享
随机推荐
koa框架(一)
云服务器基于 SSH 协议实现免密登录
Btree索引和Hash索引
这款可视化工具神器,更直观易用!太爱了
What would happen if disconnecting during the process of TCP connection?
OneNote 教程,如何在 OneNote 中整理笔记本?
Apache Doris支持的数据类型详解
蓝帽杯 2022 web/misc writeup
【Linux】宝塔面板设置MySQL慢查询日志,未走索引日志
有趣并发性能分享:线程池为什么设计成这样?
面试官: AMS在Android起到什么作用,简单的分析下Android的源码
【软件测试】2022年最火的十大测试工具,你掌握了几个
MUI框架开发app中出现的问题
实例052:按位或
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
实例049:lambda
(PC+WAP)带手机端pbootcms模板园林景观类网站
如何利用fiddler连接手机抓包APP
留言有奖|OpenBMB x 清华大学NLP:大模型公开课更新完结!
二叉树 | 迭代遍历 | leecode刷题笔记