fanxp 2 months ago
parent
commit
f2a80612ca

+ 6 - 2
src/main/java/com/xjrsoft/common/interceptor/RateLimitInterceptor.java

@@ -5,6 +5,7 @@ import cn.hutool.core.convert.Convert;
 import com.xjrsoft.common.constant.GlobalConstant;
 import com.xjrsoft.common.enums.RoleEnum;
 import com.xjrsoft.common.utils.RedisUtil;
+import com.xjrsoft.config.RateLimitConfig;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.web.servlet.HandlerInterceptor;
@@ -19,11 +20,14 @@ public class RateLimitInterceptor implements HandlerInterceptor {
     @Autowired
     private RedisUtil redisUtil;
 
+    @Autowired
+    private RateLimitConfig rateLimitConfig;
+
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-        if (isLimiting()) {
+        if (rateLimitConfig.isEnabled() && isLimiting()) {
             // 暂停5秒
-            Thread.sleep(5000);
+            Thread.sleep(rateLimitConfig.getTimeout());
             System.out.println("触发限流");
         }
         return true;

+ 19 - 0
src/main/java/com/xjrsoft/config/RateLimitConfig.java

@@ -0,0 +1,19 @@
+package com.xjrsoft.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Data
+@Component
+@ConfigurationProperties("xjrsoft.ratelimit")
+public class RateLimitConfig {
+    /**
+     * 是否开启
+     */
+    private boolean enabled;
+    /**
+     * 限流时间(秒)
+     */
+    private int timeout;
+}

+ 5 - 0
src/main/resources/application-dev.yml

@@ -58,6 +58,11 @@ mqtt:
   qos: 1
 
 xjrsoft:
+  # 学生限流配置
+  ratelimit:
+    enabled: false #是否开启限流
+    timeout: 5000 #限流时间(秒)
+  #  短信验证码  有效期 单位分钟
   oss:
     #enabled: true
     cloud-type: minio

+ 4 - 0
src/main/resources/application-pre.yml

@@ -43,6 +43,10 @@ mqtt:
   qos: 1
 
 xjrsoft:
+  # 学生限流配置
+  ratelimit:
+    enabled: false #是否开启限流
+    timeout: 5000 #限流时间(秒)
   oss:
     #enabled: true
     cloud-type: minio

+ 4 - 0
src/main/resources/application-prod.yml

@@ -42,6 +42,10 @@ mqtt:
 
 
 xjrsoft:
+  # 学生限流配置
+  ratelimit:
+    enabled: false #是否开启限流
+    timeout: 5000 #限流时间(秒)
   oss:
     #enabled: true
     cloud-type: minio