Commit b96a7bde authored by 技术部-任文超's avatar 技术部-任文超

应用Redis可选参数

parent 338a75e2
...@@ -603,19 +603,41 @@ public class UserController implements IBaseController { ...@@ -603,19 +603,41 @@ public class UserController implements IBaseController {
}*/ }*/
String ipv4 = getIp(); String ipv4 = getIp();
if (StringUtils.isNotBlank(ipv4) && !ValidationUtil.validateLocalIpv4(ipv4)) { if (StringUtils.isNotBlank(ipv4) && !ValidationUtil.validateLocalIpv4(ipv4)) {
String ipv4Key = getIpKey(getIp()); String ipv4Key = getIpKey(ipv4);
if(!stringRedisTemplate.hasKey(getIpKey(getIp()))){ if(!stringRedisTemplate.hasKey(ipv4Key)){
// 计数周期5分钟 // 计数周期1分钟
stringRedisTemplate.opsForValue().set(ipv4Key, String.valueOf(0), Constants.IPV4_COUNT_MINUTES, TimeUnit.MINUTES); stringRedisTemplate.opsForValue().set(ipv4Key, String.valueOf(0), Constants.IPV4_COUNT_MINUTES, TimeUnit.MINUTES);
} }
Long count = stringRedisTemplate.opsForValue().increment(ipv4Key, 1L); Long count = stringRedisTemplate.opsForValue().increment(ipv4Key, 1L);
LOGGER.info("Count for Locked ip access:{}, count={}", ipv4, count); LOGGER.info("Lock_ipv4: count ip access: ip={}, count={}", ipv4, count);
if (count >= Constants.IPV4_LOCK_ON_COUNTS){ lockIpv4(ipv4, count);
String lockIpv4Key = getLockIpv4Key(ipv4); }
stringRedisTemplate.opsForValue().set(lockIpv4Key, Boolean.TRUE.toString(), Constants.IPV4_LOCK_MINUTES, TimeUnit.MINUTES); }
LOGGER.info("Locked ip access:{}, error overstep {} times in {} minutes, do lock {} minutes", ipv4, Constants.IPV4_LOCK_ON_COUNTS, Constants.IPV4_COUNT_MINUTES, Constants.IPV4_LOCK_MINUTES);
} /**
* 锁定IPV4
* @param ip - 目标ip
* @param count - 错误计数
*/
private void lockIpv4(String ip, long count){
// 每分钟计数阈值
long counts = Constants.IPV4_LOCK_ON_COUNTS;
String redisCounts = stringRedisTemplate.opsForValue().get(Constants.IPV4_LOCK_ON_COUNTS_REDIS);
if(StringUtils.isNumeric(redisCounts) && Integer.valueOf(redisCounts) > 0){
counts = Integer.valueOf(redisCounts);
}
if(count < counts){
return;
}
// 锁定时长
long minutes = Constants.IPV4_LOCK_MINUTES;
String redisMinutes = stringRedisTemplate.opsForValue().get(Constants.IPV4_LOCK_ON_COUNTS_REDIS);
if(StringUtils.isNumeric(redisMinutes) && Integer.valueOf(redisMinutes) > 0){
minutes = Integer.valueOf(redisMinutes);
} }
String lockIpv4Key = getLockIpv4Key(ip);
stringRedisTemplate.opsForValue().set(lockIpv4Key, Boolean.TRUE.toString(), minutes, TimeUnit.MINUTES);
LOGGER.info("Lock_ipv4: locked ip access:{}, error overstep {} times in {} minutes, do lock {} minutes", ip, counts, Constants.IPV4_COUNT_MINUTES, minutes);
} }
private final static String getIpKey(String ipv4){ private final static String getIpKey(String ipv4){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment