Authored by Xu

恶意ip单独失效

@@ -174,8 +174,7 @@ public class MaliciousIpServiceImpl implements MaliciousIpService { @@ -174,8 +174,7 @@ public class MaliciousIpServiceImpl implements MaliciousIpService {
174 if(CollectionUtils.isNotEmpty(values)){ 174 if(CollectionUtils.isNotEmpty(values)){
175 for (String string : values) { 175 for (String string : values) {
176 look = new MaliciousIpLook(); 176 look = new MaliciousIpLook();
177 - look.setIp(string);  
178 - 177 + look.setIp(string.substring("yh:mip:".length()));
179 looks.add(look); 178 looks.add(look);
180 } 179 }
181 response.setData(looks); 180 response.setData(looks);
@@ -17,9 +17,9 @@ import redis.clients.jedis.JedisPoolConfig; @@ -17,9 +17,9 @@ import redis.clients.jedis.JedisPoolConfig;
17 public class RedisReadMaliciousUtil { 17 public class RedisReadMaliciousUtil {
18 18
19 /** 19 /**
20 - * Redis set 集合,里面放的都是ip 20 + * Redis String 类型
21 */ 21 */
22 - private static final String MALICIOUS_IP = "yh:malicious:ips"; 22 + private static final String MALICIOUS_IP = "yh:mip:*";
23 23
24 private JedisPool pool = null; 24 private JedisPool pool = null;
25 25
@@ -63,7 +63,7 @@ public class RedisReadMaliciousUtil { @@ -63,7 +63,7 @@ public class RedisReadMaliciousUtil {
63 */ 63 */
64 public Set<String> getIps() { 64 public Set<String> getIps() {
65 Jedis jedis = getJedis(); 65 Jedis jedis = getJedis();
66 - Set<String> value = jedis.smembers(MALICIOUS_IP); 66 + Set<String> value = jedis.keys(MALICIOUS_IP);
67 returnResource(jedis); 67 returnResource(jedis);
68 return value; 68 return value;
69 } 69 }
1 package com.monitor.other.maliciousip.util; 1 package com.monitor.other.maliciousip.util;
2 2
  3 +import org.slf4j.Logger;
  4 +import org.slf4j.LoggerFactory;
  5 +
3 import redis.clients.jedis.Jedis; 6 import redis.clients.jedis.Jedis;
4 import redis.clients.jedis.JedisPool; 7 import redis.clients.jedis.JedisPool;
5 import redis.clients.jedis.JedisPoolConfig; 8 import redis.clients.jedis.JedisPoolConfig;
@@ -10,11 +13,20 @@ import redis.clients.jedis.JedisPoolConfig; @@ -10,11 +13,20 @@ import redis.clients.jedis.JedisPoolConfig;
10 * 13 *
11 */ 14 */
12 public class RedisWriteUtil { 15 public class RedisWriteUtil {
  16 + /**
  17 + * 日志接口
  18 + */
  19 + private Logger logger = LoggerFactory.getLogger(getClass());
13 20
14 /** 21 /**
15 * Redis set 集合,里面放的都是ip 22 * Redis set 集合,里面放的都是ip
16 */ 23 */
17 - private static final String MALICIOUS_IP = "yh:malicious:ips"; 24 + private static final String MALICIOUS_IP_HASH = "yh:malicious:ips";
  25 +
  26 + /**
  27 + * Redis String 类型
  28 + */
  29 + private static final String MALICIOUS_IP = "yh:mip:";
18 30
19 /** 31 /**
20 * 黑名单失效时间24小时 32 * 黑名单失效时间24小时
@@ -101,44 +113,69 @@ public class RedisWriteUtil { @@ -101,44 +113,69 @@ public class RedisWriteUtil {
101 * @return 113 * @return
102 */ 114 */
103 public void setIps(String []ips) { 115 public void setIps(String []ips) {
  116 + try{
  117 + if((null == ips) || (0 == ips.length)){
  118 + return;
  119 + }
104 Jedis jedis_qq1 = getJedis_qq1(); 120 Jedis jedis_qq1 = getJedis_qq1();
105 Jedis jedis_qq2 = getJedis_qq2(); 121 Jedis jedis_qq2 = getJedis_qq2();
106 Jedis jedis_aws1 = getJedis_aws1(); 122 Jedis jedis_aws1 = getJedis_aws1();
107 Jedis jedis_aws2 = getJedis_aws2(); 123 Jedis jedis_aws2 = getJedis_aws2();
108 124
109 - jedis_qq1.sadd(MALICIOUS_IP, ips);  
110 - jedis_qq1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
111 - jedis_qq2.sadd(MALICIOUS_IP, ips);  
112 - jedis_qq2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
113 - jedis_aws1.sadd(MALICIOUS_IP, ips);  
114 - jedis_aws1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
115 - jedis_aws2.sadd(MALICIOUS_IP, ips);  
116 - jedis_aws2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE); 125 + //此处用于查询
  126 + /*jedis_aws1.sadd(MALICIOUS_IP_HASH, ips);
  127 + jedis_aws1.expire(MALICIOUS_IP_HASH, MALICIOUS_IP_EXPIRE);*/
  128 +
  129 + //此处用于拦截
  130 + for (String ip : ips) {
  131 + jedis_qq1.set(MALICIOUS_IP + ip, "0");
  132 + jedis_qq1.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
  133 + jedis_qq2.set(MALICIOUS_IP + ip, "0");
  134 + jedis_qq2.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
  135 + jedis_aws1.set(MALICIOUS_IP + ip, "0");
  136 + jedis_aws1.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
  137 + jedis_aws2.set(MALICIOUS_IP + ip, "0");
  138 + jedis_aws2.expire(MALICIOUS_IP + ip, MALICIOUS_IP_EXPIRE);
  139 + }
117 140
118 returnResource(jedis_qq1); 141 returnResource(jedis_qq1);
119 returnResource(jedis_qq2); 142 returnResource(jedis_qq2);
120 returnResource(jedis_aws1); 143 returnResource(jedis_aws1);
121 returnResource(jedis_aws2); 144 returnResource(jedis_aws2);
  145 + }catch(Exception e){
  146 + logger.error(" - RedisWriteUtil - setIps - error", e);
  147 + }
122 } 148 }
123 149
124 public void removeIp(String[] ips){ 150 public void removeIp(String[] ips){
  151 + try{
  152 + if((null == ips) || (0 == ips.length)){
  153 + return;
  154 + }
125 Jedis jedis_qq1 = getJedis_qq1(); 155 Jedis jedis_qq1 = getJedis_qq1();
126 Jedis jedis_qq2 = getJedis_qq2(); 156 Jedis jedis_qq2 = getJedis_qq2();
127 Jedis jedis_aws1 = getJedis_aws1(); 157 Jedis jedis_aws1 = getJedis_aws1();
128 Jedis jedis_aws2 = getJedis_aws2(); 158 Jedis jedis_aws2 = getJedis_aws2();
129 159
130 - jedis_qq1.srem(MALICIOUS_IP, ips);  
131 - jedis_qq1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
132 - jedis_qq2.srem(MALICIOUS_IP, ips);  
133 - jedis_qq2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
134 - jedis_aws1.srem(MALICIOUS_IP, ips);  
135 - jedis_aws1.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE);  
136 - jedis_aws2.srem(MALICIOUS_IP, ips);  
137 - jedis_aws2.expire(MALICIOUS_IP, MALICIOUS_IP_EXPIRE); 160 + //此处用于查询
  161 + /*jedis_aws1.srem(MALICIOUS_IP_HASH, ips);
  162 + jedis_aws1.expire(MALICIOUS_IP_HASH, MALICIOUS_IP_EXPIRE);*/
  163 +
  164 + for (int index = 0; index < ips.length; index ++){
  165 + ips[index] = MALICIOUS_IP + ips[index];
  166 + }
  167 + //此处用于拦截
  168 + jedis_qq1.del(ips);
  169 + jedis_qq2.del(ips);
  170 + jedis_aws1.del(ips);
  171 + jedis_aws2.del(ips);
138 172
139 returnResource(jedis_qq1); 173 returnResource(jedis_qq1);
140 returnResource(jedis_qq2); 174 returnResource(jedis_qq2);
141 returnResource(jedis_aws1); 175 returnResource(jedis_aws1);
142 returnResource(jedis_aws2); 176 returnResource(jedis_aws2);
  177 + }catch(Exception e){
  178 + logger.error(" - RedisWriteUtil - setIps - error", e);
  179 + }
143 } 180 }
144 } 181 }