Commit 4b7efb7baea5b99d087b8d1c95eff704c053ab79

Authored by 谭毅彬
1 parent 877d20a6

访问限制添加优化

Signed-off-by: TanYibin <5491541@qq.com>
huaheng-wms-core/pom.xml
... ... @@ -42,7 +42,7 @@
42 42 <groupId>org.redisson</groupId>
43 43 <artifactId>redisson</artifactId>
44 44 </dependency>
45   -
  45 +
46 46 <dependency>
47 47 <groupId>cn.monitor4all</groupId>
48 48 <artifactId>log-record-starter</artifactId>
... ... @@ -54,6 +54,11 @@
54 54 <version>1.69</version>
55 55 </dependency>
56 56 <dependency>
  57 + <groupId>cn.hutool</groupId>
  58 + <artifactId>hutool-all</artifactId>
  59 + <version>5.4.7</version>
  60 + </dependency>
  61 + <dependency>
57 62 <groupId>com.googlecode.log4jdbc</groupId>
58 63 <artifactId>log4jdbc</artifactId>
59 64 <version>1.2</version>
... ... @@ -128,7 +133,7 @@
128 133 <artifactId>spring-boot-configuration-processor</artifactId>
129 134 <optional>true</optional>
130 135 </dependency>
131   - <!--获取apk信息-->
  136 + <!--获取apk信息 -->
132 137 <dependency>
133 138 <groupId>net.dongliu</groupId>
134 139 <artifactId>apk-parser</artifactId>
... ...
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java
... ... @@ -46,7 +46,7 @@ public class TestController extends HuahengBaseController {
46 46 @Autowired
47 47 private ApplicationConfig applicationConfig;
48 48  
49   -// @AccessLimit(seconds = 1, maxCount = 10)
  49 + @AccessLimit(seconds = 10, maxCount = 2)
50 50 @PostMapping(value = "/testLimiter")
51 51 public Result<?> testLimiter(@RequestBody Map<String, String> paramMap) {
52 52 return Result.OK();
... ...
huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengRedisUtil.java
... ... @@ -3,9 +3,9 @@ package org.jeecg.utils;
3 3 import java.util.concurrent.TimeUnit;
4 4  
5 5 import javax.annotation.Nonnull;
6   -import javax.annotation.Resource;
7 6  
8   -import org.springframework.data.redis.core.RedisTemplate;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.data.redis.core.StringRedisTemplate;
9 9 import org.springframework.stereotype.Component;
10 10 import org.springframework.util.StringUtils;
11 11  
... ... @@ -23,8 +23,8 @@ import lombok.extern.slf4j.Slf4j;
23 23 @Component
24 24 public class HuahengRedisUtil {
25 25  
26   - @Resource
27   - private RedisTemplate<String, String> redisTemplate;
  26 + @Autowired
  27 + private StringRedisTemplate redisTemplate;
28 28  
29 29 private static String toJson(Object obj) {
30 30 if (obj == null) {
... ...
huaheng-wms-core/src/main/java/org/jeecg/utils/interceptor/AccessLimitInterceptor.java
... ... @@ -37,26 +37,23 @@ public class AccessLimitInterceptor extends HandlerInterceptorAdapter {
37 37 @Override
38 38 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
39 39 if (handler instanceof HandlerMethod) {
40   - HandlerMethod hm = (HandlerMethod)handler;
  40 + HandlerMethod handlerMethod = (HandlerMethod)handler;
41 41 // 获取方法中的注解,看是否有该注解
42   - AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
  42 + AccessLimit accessLimit = handlerMethod.getMethodAnnotation(AccessLimit.class);
43 43 if (accessLimit == null) {
44 44 return true;
45 45 }
46 46 int seconds = accessLimit.seconds();
47 47 int maxCount = accessLimit.maxCount();
48   - String requestURI = request.getRequestURI();
49   - log.info(ServletUtil.getBody(request));
50   - Integer count = huahengRedisUtil.get(requestURI, Integer.class);
51   - if (count == null) {
52   - // 第一次访问
53   - huahengRedisUtil.set(requestURI, 1, seconds);
54   - } else if (count < maxCount) {
55   - // 加1
56   - huahengRedisUtil.incr(requestURI, seconds);
57   - } else {
58   - log.error("{},超出访问频次限制。限制频次:{}次/{}秒", requestURI, maxCount, seconds);
59   - throw new JeecgBootException("超出访问频次限制");
  48 + String accessLimitKey = "Access_Limit_" + request.getRequestURI();
  49 + synchronized (accessLimitKey) {
  50 + Integer count = huahengRedisUtil.get(accessLimitKey, Integer.class);
  51 + if (count == null || count < maxCount) {
  52 + huahengRedisUtil.incr(accessLimitKey, seconds);
  53 + } else {
  54 + log.error("{},超出访问频次限制。限制频次:{}秒{}次", request.getRequestURI(), maxCount, seconds);
  55 + throw new JeecgBootException("超出访问频次限制");
  56 + }
60 57 }
61 58 }
62 59 return true;
... ...