当前位置:网站首页>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;}}}边栏推荐
猜你喜欢

信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全

高精度减法

CFdiv2-Common Number-(奇偶数二分+规律)

3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)

Redis
![68: Chapter 6: Develop article services: 1: Content sorting; article table introduction; creating [article] article services;](/img/95/7f21ecda19030c2faecbe373893d66.png)
68: Chapter 6: Develop article services: 1: Content sorting; article table introduction; creating [article] article services;

Leave a message with a prize | OpenBMB x Tsinghua University NLP: The update of the large model open class is complete!

带你造轮子,自定义一个随意拖拽可吸边的View

手机端出现Z-Fighting现象

MySQL: MySQL Cluster - Principle and Configuration of Master-Slave Replication
随机推荐
HGAME 2022 Final writeup
Flink(Pometheus监控)
EL表达式
ITK 读取一个目录中的一个序列,然后改变头信息,将多张dcm图像写成一个dcm文件。
web项目访问引用jar内部的静态资源
有趣并发性能分享:线程池为什么设计成这样?
亲测有效|处理风控数据特征缺失的一种方法
Take you to build a wheel and customize a View that can be dragged and sucked at will
浅谈cors
数学建模准备知识
【640. Solving Equations】
【秋招】【更新中ing】手撕代码系列
Research on multi-element N-k fault model of power system based on AC power flow (implemented by Matlab code) [Power System Fault]
常用代码扩展点设计方式
68: Chapter 6: Develop article services: 1: Content sorting; article table introduction; creating [article] article services;
Detailed installation steps and environment configuration of geemap
Btree index and Hash index
瑞幸咖啡第二季营收33亿:门店达7195家 更换CFO
完全自定义MaterialButtonToggleGroup颜色。
STL-deque