ToB企服应用市场:ToB评测及商务社交产业平台

标题: 借助redis实现对IP限流 [打印本页]

作者: 干翻全岛蛙蛙    时间: 2022-9-16 17:23
标题: 借助redis实现对IP限流
背景: 若依前后端分离项目(vue+springboot+springmvc+mybatis),redis。 需求: 借助redis实现对IP限流。实现:参考 
 https://blog.csdn.net/qq_33762302/article/details/116258617
 代码如下: IPLimiter.java 定义注解类,将注解定义在需要分流IP的接口上
  1. 1 import java.lang.annotation.*;
  2. 2
  3. 3 @Target(ElementType.METHOD)
  4. 4 @Retention(RetentionPolicy.RUNTIME)
  5. 5 @Documented
  6. 6 public @interface IpLimiter {
  7. 7     /**
  8. 8      * 放行ip
  9. 9      */
  10. 10     String[] ipAdress() default {""};
  11. 11     /**
  12. 12      * 单位时间限制通过请求数
  13. 13      */
  14. 14     long limit() default 10;
  15. 15     /**
  16. 16      * 单位时间,单位秒
  17. 17      */
  18. 18     long time() default 1;
  19. 19     /**
  20. 20      * 达到限流提示语
  21. 21      */
  22. 22     String message();
  23. 23
  24. 24     /**
  25. 25      * 是否锁住IP的同时锁住URI
  26. 26      */
  27. 27     boolean lockUri() default false;
  28. 28 }
复制代码
 
IpLimterHandler.java 注解AOP处理。
  1.   1 import com.missionex.common.annotation.IpLimiter;
  2.   2 import com.missionex.common.core.domain.AjaxResult;
  3.   3 import com.missionex.common.utils.DateUtils;
  4.   4 import com.missionex.common.utils.SecurityUtils;
  5.   5 import com.missionex.common.utils.http.IPAddressUtils;
  6.   6 import org.aspectj.lang.ProceedingJoinPoint;
  7.   7 import org.aspectj.lang.Signature;
  8.   8 import org.aspectj.lang.annotation.Around;
  9.   9 import org.aspectj.lang.annotation.Aspect;
  10. 10 import org.aspectj.lang.reflect.MethodSignature;
  11. 11 import org.slf4j.Logger;
  12. 12 import org.slf4j.LoggerFactory;
  13. 13 import org.springframework.beans.factory.annotation.Autowired;
  14. 14 import org.springframework.core.io.ClassPathResource;
  15. 15 import org.springframework.data.redis.core.StringRedisTemplate;
  16. 16 import org.springframework.data.redis.core.script.DefaultRedisScript;
  17. 17 import org.springframework.scripting.support.ResourceScriptSource;
  18. 18 import org.springframework.stereotype.Component;
  19. 19 import org.springframework.web.context.request.RequestContextHolder;
  20. 20 import org.springframework.web.context.request.ServletRequestAttributes;
  21. 21
  22. 22 import javax.annotation.PostConstruct;
  23. 23 import javax.servlet.http.HttpServletRequest;
  24. 24 import java.util.ArrayList;
  25. 25 import java.util.List;
  26. 26
  27. 27 @Aspect
  28. 28 @Component
  29. 29 public class IpLimterHandler {
  30. 30
  31. 31     private static final Logger LOGGER = LoggerFactory.getLogger("request-limit");
  32. 32
  33. 33     @Autowired
  34. 34     StringRedisTemplate redisTemplate;
  35. 35
  36. 36
  37. 37     /**
  38. 38      * getRedisScript 读取脚本工具类
  39. 39      * 这里设置为Long,是因为ipLimiter.lua 脚本返回的是数字类型
  40. 40      */
  41. 41     private DefaultRedisScript<Long> getRedisScript;
  42. 42
  43. 43     @PostConstruct
  44. 44     public void init() {
  45. 45         getRedisScript = new DefaultRedisScript<>();
  46. 46         getRedisScript.setResultType(Long.class);
  47. 47         getRedisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("ipLimiter.lua")));
  48. 48         LOGGER.info("IpLimterHandler[分布式限流处理器]脚本加载完成");
  49. 49     }
  50. 50
  51. 51     /**
  52. 52      * 这个切点可以不要,因为下面的本身就是个注解
  53. 53      */
  54. 54 //    @Pointcut("@annotation(com.jincou.iplimiter.annotation.IpLimiter)")
  55. 55 //    public void rateLimiter() {}
  56. 56
  57. 57     /**
  58. 58      * 如果保留上面这个切点,那么这里可以写成
  59. 59      * @Around("rateLimiter()&&@annotation(ipLimiter)")
  60. 60      */
  61. 61     @Around("@annotation(ipLimiter)")
  62. 62     public Object around(ProceedingJoinPoint proceedingJoinPoint, IpLimiter ipLimiter) throws Throwable {
  63. 63         if (LOGGER.isDebugEnabled()) {
  64. 64             LOGGER.debug("IpLimterHandler[分布式限流处理器]开始执行限流操作");
  65. 65         }
  66. 66         String userIp = null;
  67. 67         String requestURI = null;
  68. 68         try {
  69. 69             // 获取请求信息
  70. 70             HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  71. 71             requestURI = request.getRequestURI();
  72. 72             String requestMethod = request.getMethod();
  73. 73             String remoteAddr = request.getRemoteAddr();
  74. 74             // 获取请求用户IP
  75. 75             userIp = IPAddressUtils.getIpAdrress(request);
  76. 76             if (userIp == null) {
  77. 77                 return AjaxResult.error("运行环境存在风险");
  78. 78             }
  79. 79         } catch (Exception e) {
  80. 80             LOGGER.error("获取request出错=>" + e.getMessage());
  81. 81             if (userIp == null) {
  82. 82                 return AjaxResult.error("运行环境存在风险");
  83. 83             }
  84. 84         }
  85. 85         Signature signature = proceedingJoinPoint.getSignature();
  86. 86         if (!(signature instanceof MethodSignature)) {
  87. 87             throw new IllegalArgumentException("the Annotation @IpLimter must used on method!");
  88. 88         }
  89. 89         /**
  90. 90          * 获取注解参数
  91. 91          */
  92. 92         // 放行模块IP
  93. 93         String[] limitIp = ipLimiter.ipAdress();
  94. 94         int len;
  95. 95         if (limitIp != null && (len = limitIp.length) != 0) {
  96. 96             for (int i = 0; i < len; i++) {
  97. 97                 if (limitIp[i].equals(userIp)) {
  98. 98                     return proceedingJoinPoint.proceed();
  99. 99                 }
  100. 100             }
  101. 101         }
  102. 102         // 限流阈值
  103. 103         long limitTimes = ipLimiter.limit();
  104. 104         // 限流超时时间
  105. 105         long expireTime = ipLimiter.time();
  106. 106         boolean lockUri = ipLimiter.lockUri();
  107. 107         if (LOGGER.isDebugEnabled()) {
  108. 108             LOGGER.debug("IpLimterHandler[分布式限流处理器]参数值为-limitTimes={},limitTimeout={}", limitTimes, expireTime);
  109. 109         }
  110. 110         // 限流提示语
  111. 111         String message = ipLimiter.message();
  112. 112         /**
  113. 113          * 执行Lua脚本
  114. 114          */
  115. 115         List<String> ipList = new ArrayList();
  116. 116         // 设置key值为注解中的值
  117. 117         if (lockUri) {
  118. 118             ipList.add(userIp+requestURI);
  119. 119         } else {
  120. 120             ipList.add(userIp);
  121. 121         }
  122. 122         /**
  123. 123          * 调用脚本并执行
  124. 124          */
  125. 125         try {
  126. 126             Object x = redisTemplate.execute(getRedisScript, ipList, expireTime+"", limitTimes+"");
  127. 127             Long result = (Long) x;
  128. 128             if (result == 0) {
  129. 129                 Long userId = null;
  130. 130                 try {
  131. 131                     userId = SecurityUtils.getLoginUser().getAppUser().getId();
  132. 132                 } catch (Exception e) {
  133. 133
  134. 134                 }
  135. 135                 LOGGER.info("[分布式限流处理器]限流执行结果-ip={}-接口={}-用户ID={}-result={}-time={},已被限流", userIp,requestURI == null?"未知":requestURI
  136. 136                         ,userId==null?"用户未登录":userId,result, DateUtils.getTime());
  137. 137                 // 达到限流返回给前端信息
  138. 138                 return AjaxResult.error(message);
  139. 139             }
  140. 140             if (LOGGER.isDebugEnabled()) {
  141. 141                 LOGGER.debug("IpLimterHandler[分布式限流处理器]限流执行结果-result={},请求[正常]响应", result);
  142. 142             }
  143. 143             return proceedingJoinPoint.proceed();
  144. 144         } catch (Exception e) {
  145. 145             LOGGER.error("限流错误",e);
  146. 146             return proceedingJoinPoint.proceed();
  147. 147         }
  148. 148
  149. 149     }
  150. 150 }
复制代码
 
 
ipLimiter.lua 脚本,放在resources文件夹中。
  1. 1 -获取KEY
  2. 2 local key1 = KEYS[1]
  3. 3
  4. 4 local val = redis.call('incr', key1)
  5. 5 local ttl = redis.call('ttl', key1)
  6. 6
  7. 7 --获取ARGV内的参数并打印
  8. 8 local expire = ARGV[1]
  9. 9 local times = ARGV[2]
  10. 10
  11. 11 redis.log(redis.LOG_DEBUG,tostring(times))
  12. 12 redis.log(redis.LOG_DEBUG,tostring(expire))
  13. 13
  14. 14 redis.log(redis.LOG_NOTICE, "incr "..key1.." "..val);
  15. 15 if val == 1 then
  16. 16     redis.call('expire', key1, tonumber(expire))
  17. 17 else
  18. 18     if ttl == -1 then
  19. 19         redis.call('expire', key1, tonumber(expire))
  20. 20     end
  21. 21 end
  22. 22
  23. 23 if val > tonumber(times) then
  24. 24     return 0
  25. 25 end
  26. 26 return 1
复制代码
 
RedisConfig.java redis配置(该配置继承了CachingConfigurerSupport)中对写入redis的数据的序列化。
如果使用IP锁的时候,错误出现在了AOP中的使用脚本写入redis的时候(像什么Long无法转String的错误),基本是这边序列化没配好。
  1. 1    @Bean
  2. 2     @SuppressWarnings(value = { "unchecked", "rawtypes" })
  3. 3     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
  4. 4     {
  5. 5         RedisTemplate<Object, Object> template = new RedisTemplate<>();
  6. 6         template.setConnectionFactory(connectionFactory);
  7. 7         FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
  8. 8         // 使用StringRedisSerializer来序列化和反序列化redis的key值
  9. 9         template.setKeySerializer(new StringRedisSerializer());
  10. 10         template.setValueSerializer(serializer);
  11. 11         // Hash的key也采用StringRedisSerializer的序列化方式
  12. 12         template.setHashKeySerializer(new StringRedisSerializer());
  13. 13         template.setHashValueSerializer(serializer);
  14. 14         template.afterPropertiesSet();
  15. 15         return template;
  16. 16     }
复制代码
 
使用示范。
  1. 1     @PostMapping("/test")
  2. 2     @IpLimiter(limit = 2, time = 5, message = "您访问过于频繁,请稍候访问",lockUri = true)
  3. 3     public AjaxResult test(@RequestBody Map map){
  4. 4         //代码......
  5. 5     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4