Commit 4b7efb7baea5b99d087b8d1c95eff704c053ab79
1 parent
877d20a6
访问限制添加优化
Signed-off-by: TanYibin <5491541@qq.com>
Showing
4 changed files
with
23 additions
and
21 deletions
huaheng-wms-core/pom.xml
@@ -42,7 +42,7 @@ | @@ -42,7 +42,7 @@ | ||
42 | <groupId>org.redisson</groupId> | 42 | <groupId>org.redisson</groupId> |
43 | <artifactId>redisson</artifactId> | 43 | <artifactId>redisson</artifactId> |
44 | </dependency> | 44 | </dependency> |
45 | - | 45 | + |
46 | <dependency> | 46 | <dependency> |
47 | <groupId>cn.monitor4all</groupId> | 47 | <groupId>cn.monitor4all</groupId> |
48 | <artifactId>log-record-starter</artifactId> | 48 | <artifactId>log-record-starter</artifactId> |
@@ -54,6 +54,11 @@ | @@ -54,6 +54,11 @@ | ||
54 | <version>1.69</version> | 54 | <version>1.69</version> |
55 | </dependency> | 55 | </dependency> |
56 | <dependency> | 56 | <dependency> |
57 | + <groupId>cn.hutool</groupId> | ||
58 | + <artifactId>hutool-all</artifactId> | ||
59 | + <version>5.4.7</version> | ||
60 | + </dependency> | ||
61 | + <dependency> | ||
57 | <groupId>com.googlecode.log4jdbc</groupId> | 62 | <groupId>com.googlecode.log4jdbc</groupId> |
58 | <artifactId>log4jdbc</artifactId> | 63 | <artifactId>log4jdbc</artifactId> |
59 | <version>1.2</version> | 64 | <version>1.2</version> |
@@ -128,7 +133,7 @@ | @@ -128,7 +133,7 @@ | ||
128 | <artifactId>spring-boot-configuration-processor</artifactId> | 133 | <artifactId>spring-boot-configuration-processor</artifactId> |
129 | <optional>true</optional> | 134 | <optional>true</optional> |
130 | </dependency> | 135 | </dependency> |
131 | - <!--获取apk信息--> | 136 | + <!--获取apk信息 --> |
132 | <dependency> | 137 | <dependency> |
133 | <groupId>net.dongliu</groupId> | 138 | <groupId>net.dongliu</groupId> |
134 | <artifactId>apk-parser</artifactId> | 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,7 +46,7 @@ public class TestController extends HuahengBaseController { | ||
46 | @Autowired | 46 | @Autowired |
47 | private ApplicationConfig applicationConfig; | 47 | private ApplicationConfig applicationConfig; |
48 | 48 | ||
49 | -// @AccessLimit(seconds = 1, maxCount = 10) | 49 | + @AccessLimit(seconds = 10, maxCount = 2) |
50 | @PostMapping(value = "/testLimiter") | 50 | @PostMapping(value = "/testLimiter") |
51 | public Result<?> testLimiter(@RequestBody Map<String, String> paramMap) { | 51 | public Result<?> testLimiter(@RequestBody Map<String, String> paramMap) { |
52 | return Result.OK(); | 52 | return Result.OK(); |
huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengRedisUtil.java
@@ -3,9 +3,9 @@ package org.jeecg.utils; | @@ -3,9 +3,9 @@ package org.jeecg.utils; | ||
3 | import java.util.concurrent.TimeUnit; | 3 | import java.util.concurrent.TimeUnit; |
4 | 4 | ||
5 | import javax.annotation.Nonnull; | 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 | import org.springframework.stereotype.Component; | 9 | import org.springframework.stereotype.Component; |
10 | import org.springframework.util.StringUtils; | 10 | import org.springframework.util.StringUtils; |
11 | 11 | ||
@@ -23,8 +23,8 @@ import lombok.extern.slf4j.Slf4j; | @@ -23,8 +23,8 @@ import lombok.extern.slf4j.Slf4j; | ||
23 | @Component | 23 | @Component |
24 | public class HuahengRedisUtil { | 24 | public class HuahengRedisUtil { |
25 | 25 | ||
26 | - @Resource | ||
27 | - private RedisTemplate<String, String> redisTemplate; | 26 | + @Autowired |
27 | + private StringRedisTemplate redisTemplate; | ||
28 | 28 | ||
29 | private static String toJson(Object obj) { | 29 | private static String toJson(Object obj) { |
30 | if (obj == null) { | 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,26 +37,23 @@ public class AccessLimitInterceptor extends HandlerInterceptorAdapter { | ||
37 | @Override | 37 | @Override |
38 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | 38 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
39 | if (handler instanceof HandlerMethod) { | 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 | if (accessLimit == null) { | 43 | if (accessLimit == null) { |
44 | return true; | 44 | return true; |
45 | } | 45 | } |
46 | int seconds = accessLimit.seconds(); | 46 | int seconds = accessLimit.seconds(); |
47 | int maxCount = accessLimit.maxCount(); | 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 | return true; | 59 | return true; |