andy hai 1 ano
pai
achega
f153453253

+ 5 - 0
pom.xml

@@ -105,6 +105,11 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
+		<dependency>
+			<groupId>com.google.guava</groupId>
+			<artifactId>guava</artifactId>
+			<version>31.1-jre</version>
+		</dependency>
 	</dependencies>
 
 	<build>

+ 33 - 8
src/main/java/com/scbfkj/uni/api/LogAop.java

@@ -1,5 +1,6 @@
 package com.scbfkj.uni.api;
 
+import com.google.common.util.concurrent.RateLimiter;
 import com.scbfkj.uni.library.DataFormatUtil;
 import com.scbfkj.uni.library.RequestUtil;
 import com.scbfkj.uni.library.UniReturnUtil;
@@ -19,6 +20,9 @@ import org.springframework.web.context.request.ServletRequestAttributes;
 
 import java.time.LocalDateTime;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
 
 @Component
 @Aspect
@@ -28,7 +32,8 @@ public class LogAop {
     @Resource
     private SecurityService securityService;
 
-    private static List<Map<String, Object>> apiInfos;
+
+    private static Map<String, RateLimiter> rateLimiterMap=new HashMap<>();
 
 
     @Around(value = "within( com.scbfkj.uni.api.*Api)")
@@ -44,19 +49,43 @@ public class LogAop {
 
 //        请求
         String uri = request.getRequestURI();
+
+
 //        请求参数
         Object[] args = joinPoint.getArgs();
         Object returnData = null;
         String message = null;
 
         try {
+            List<Map<String, Object>> ratelimitruleList = DataBase.query(Config.securityConnectionStr, "select * from ratelimitrule where 1=?", Collections.singletonList(new Object[]{1}));
+            Optional<Map<String, Object>> optional = ratelimitruleList.stream().filter(it -> {
+                Object pathMatch = it.get("pathmatch");
+                if (Objects.isNull(pathMatch)) return true;
+                return uri.matches(pathMatch.toString());
+            }).findFirst();
+            if (optional.isPresent()) {
+                Map<String, Object> map = optional.get();
+                String pathMatch = map.get("pathmatch").toString();
+                if(!rateLimiterMap.containsKey(pathMatch)){
+                    String duration = map.getOrDefault("duration", 1).toString();
+                    String limitValue = map.getOrDefault("limitvalue", 100).toString();
+                    rateLimiterMap.put(pathMatch,RateLimiter.create(Double.parseDouble(limitValue),Integer.parseInt(duration), TimeUnit.SECONDS));
+                }
+                RateLimiter rateLimiter = rateLimiterMap.get(pathMatch);
+                String timeOut = map.getOrDefault("timeout", 1).toString();
+                boolean acquire = rateLimiter.tryAcquire(Integer.parseInt(timeOut), TimeUnit.SECONDS);
+                if(!acquire){
+                    message = map.getOrDefault("returnmessage", "请求频率过高,请降低请求频率").toString();
+                    return ResponseEntity.ok(UniReturnUtil.fail(message));
+                }
+            }
+
 
             if (Config.securityEnable) {
-                if (Objects.isNull(apiInfos) || apiInfos.isEmpty()) {
-                    apiInfos = DataBase.query(Config.securityConnectionStr, "select * from apiinfo", new ArrayList<>() {{
+                 List<Map<String,Object>>   apiInfos = DataBase.query(Config.securityConnectionStr, "select * from apiinfo", new ArrayList<>() {{
                         add(new Object[]{});
                     }});
-                }
+
 
                 Optional<Map<String, Object>> requestpath = apiInfos.stream().filter(it -> {
                     Object o = it.get("requestpath");
@@ -112,11 +141,7 @@ public class LogAop {
                 try {
                     put("applicationid", RequestUtil.getAppId());
                 } catch (Exception e) {
-                    if (Config.debug) {
-                        e.printStackTrace();
-                    } else {
                         System.out.println(UniReturnUtil.getMessage(e));
-                    }
                 }
             }});
         }

+ 0 - 1
src/main/java/com/scbfkj/uni/library/RequestUtil.java

@@ -45,7 +45,6 @@ public class RequestUtil {
         ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
         Object userToken = requestAttributes.getRequest().getHeader("usertoken");
         return Objects.nonNull(userToken) ? userToken.toString() : null;
-
     }
 
     public static String getAppId() throws Exception {

+ 2 - 9
src/main/java/com/scbfkj/uni/library/script/JavaScriptEngineUtil.java

@@ -126,7 +126,7 @@ public class JavaScriptEngineUtil {
         }
     });
 
-    public static Map<String, Object> invoke(Map<String, Object> datasource, String expression, List<Object> args) {
+    public static Map<String, Object> invoke(Map<String, Object> datasource, String expression, List<Object> args) throws Exception {
         datasource.put("methodName", expression);
         String key = DataFormatUtil.toString(datasource);
 
@@ -138,15 +138,8 @@ public class JavaScriptEngineUtil {
             return UniReturnUtil.fail(e);
         } finally {
             if (Objects.nonNull(javaApply)) {
-                try {
                     javaRefPool.returnObject(key, javaApply);
-                } catch (Exception e) {
-                    LoggerService.log("systemerror",new HashMap<>(){{
-                        put("occurrencetime", LocalDateTime.now());
-                        put("occurrenceaddress",null);
-                        put("detailedmessage",UniReturnUtil.getMessage(e));
-                    }});
-                }
+
             }
         }
     }

+ 2 - 9
src/main/java/com/scbfkj/uni/library/script/JsScriptEngineUtil.java

@@ -45,7 +45,7 @@ public final class JsScriptEngineUtil {
     }, new GenericKeyedObjectPoolConfig<>());
 
 
-    public static Map<String,Object> eval(String script, Object... args) {
+    public static Map<String,Object> eval(String script, Object... args) throws Exception {
         Value function = null;
         try {
         function = scriptPool.borrowObject(script);
@@ -59,15 +59,8 @@ public final class JsScriptEngineUtil {
             throw new RuntimeException(e);
         } finally {
             if (Objects.nonNull(function)) {
-                try {
                     scriptPool.returnObject(script, function);
-                } catch (Exception e) {
-                    LoggerService.log("systemerror",new HashMap<>(){{
-                        put("occurrencetime",LocalDateTime.now());
-                        put("occurrenceaddress",null);
-                        put("detailedmessage",UniReturnUtil.getMessage(e));
-                    }});
-                }
+
             }
         }
     }

+ 0 - 1
src/main/java/com/scbfkj/uni/service/SecurityService.java

@@ -454,7 +454,6 @@ public class SecurityService {
         } else {
             return UniReturnUtil.success("查询成功");
         }
-
     }
 
     public void addCode(String code, String sessionId, String appid, Long securitycodeeffective, String requestIp) throws Exception {