Commit 877d20a6b39317e96548177699251a2f95728041
1 parent
b63d003e
访问限制添加
Showing
9 changed files
with
178 additions
and
48 deletions
huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/LoginController.java
@@ -53,7 +53,6 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -53,7 +53,6 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
53 | import org.springframework.context.annotation.Bean; | 53 | import org.springframework.context.annotation.Bean; |
54 | import org.springframework.core.io.DefaultResourceLoader; | 54 | import org.springframework.core.io.DefaultResourceLoader; |
55 | import org.springframework.core.io.ResourceLoader; | 55 | import org.springframework.core.io.ResourceLoader; |
56 | -import org.springframework.data.redis.core.RedisTemplate; | ||
57 | import org.springframework.util.FileCopyUtils; | 56 | import org.springframework.util.FileCopyUtils; |
58 | import org.springframework.web.bind.annotation.GetMapping; | 57 | import org.springframework.web.bind.annotation.GetMapping; |
59 | import org.springframework.web.bind.annotation.PathVariable; | 58 | import org.springframework.web.bind.annotation.PathVariable; |
@@ -109,9 +108,6 @@ public class LoginController { | @@ -109,9 +108,6 @@ public class LoginController { | ||
109 | private ISysTenantService sysTenantService; | 108 | private ISysTenantService sysTenantService; |
110 | 109 | ||
111 | @Autowired | 110 | @Autowired |
112 | - public RedisTemplate<String, ?> redisTemplate; | ||
113 | - | ||
114 | - @Autowired | ||
115 | private ISysDictService sysDictService; | 111 | private ISysDictService sysDictService; |
116 | 112 | ||
117 | @Autowired | 113 | @Autowired |
huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java
1 | package org.jeecg.modules.system.controller; | 1 | package org.jeecg.modules.system.controller; |
2 | 2 | ||
3 | -import com.alibaba.fastjson.JSONObject; | ||
4 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
5 | -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
6 | -import lombok.extern.slf4j.Slf4j; | 3 | +import java.io.IOException; |
4 | +import java.util.ArrayList; | ||
5 | +import java.util.Arrays; | ||
6 | +import java.util.Collections; | ||
7 | +import java.util.Comparator; | ||
8 | +import java.util.HashMap; | ||
9 | +import java.util.List; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.Set; | ||
12 | + | ||
13 | +import javax.servlet.http.HttpServletRequest; | ||
14 | +import javax.servlet.http.HttpServletResponse; | ||
15 | + | ||
7 | import org.apache.commons.lang3.StringUtils; | 16 | import org.apache.commons.lang3.StringUtils; |
8 | import org.apache.shiro.SecurityUtils; | 17 | import org.apache.shiro.SecurityUtils; |
9 | import org.apache.shiro.authz.annotation.RequiresPermissions; | 18 | import org.apache.shiro.authz.annotation.RequiresPermissions; |
10 | -import org.apache.shiro.authz.annotation.RequiresRoles; | ||
11 | import org.jeecg.common.api.vo.Result; | 19 | import org.jeecg.common.api.vo.Result; |
12 | import org.jeecg.common.constant.CacheConstant; | 20 | import org.jeecg.common.constant.CacheConstant; |
13 | import org.jeecg.common.constant.CommonConstant; | 21 | import org.jeecg.common.constant.CommonConstant; |
14 | import org.jeecg.common.system.query.QueryGenerator; | 22 | import org.jeecg.common.system.query.QueryGenerator; |
15 | -import org.jeecg.utils.HuahengJwtUtil; | ||
16 | import org.jeecg.common.system.vo.LoginUser; | 23 | import org.jeecg.common.system.vo.LoginUser; |
17 | import org.jeecg.common.util.ImportExcelUtil; | 24 | import org.jeecg.common.util.ImportExcelUtil; |
18 | import org.jeecg.common.util.YouBianCodeUtil; | 25 | import org.jeecg.common.util.YouBianCodeUtil; |
@@ -24,6 +31,7 @@ import org.jeecg.modules.system.model.SysDepartTreeModel; | @@ -24,6 +31,7 @@ import org.jeecg.modules.system.model.SysDepartTreeModel; | ||
24 | import org.jeecg.modules.system.service.ISysDepartService; | 31 | import org.jeecg.modules.system.service.ISysDepartService; |
25 | import org.jeecg.modules.system.service.ISysUserDepartService; | 32 | import org.jeecg.modules.system.service.ISysUserDepartService; |
26 | import org.jeecg.modules.system.service.ISysUserService; | 33 | import org.jeecg.modules.system.service.ISysUserService; |
34 | +import org.jeecg.utils.HuahengJwtUtil; | ||
27 | import org.jeecgframework.poi.excel.ExcelImportUtil; | 35 | import org.jeecgframework.poi.excel.ExcelImportUtil; |
28 | import org.jeecgframework.poi.excel.def.NormalExcelConstants; | 36 | import org.jeecgframework.poi.excel.def.NormalExcelConstants; |
29 | import org.jeecgframework.poi.excel.entity.ExportParams; | 37 | import org.jeecgframework.poi.excel.entity.ExportParams; |
@@ -32,15 +40,21 @@ import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; | @@ -32,15 +40,21 @@ import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; | ||
32 | import org.springframework.beans.factory.annotation.Autowired; | 40 | import org.springframework.beans.factory.annotation.Autowired; |
33 | import org.springframework.cache.annotation.CacheEvict; | 41 | import org.springframework.cache.annotation.CacheEvict; |
34 | import org.springframework.data.redis.core.RedisTemplate; | 42 | import org.springframework.data.redis.core.RedisTemplate; |
35 | -import org.springframework.web.bind.annotation.*; | 43 | +import org.springframework.web.bind.annotation.GetMapping; |
44 | +import org.springframework.web.bind.annotation.RequestBody; | ||
45 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
46 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
47 | +import org.springframework.web.bind.annotation.RequestParam; | ||
48 | +import org.springframework.web.bind.annotation.RestController; | ||
36 | import org.springframework.web.multipart.MultipartFile; | 49 | import org.springframework.web.multipart.MultipartFile; |
37 | import org.springframework.web.multipart.MultipartHttpServletRequest; | 50 | import org.springframework.web.multipart.MultipartHttpServletRequest; |
38 | import org.springframework.web.servlet.ModelAndView; | 51 | import org.springframework.web.servlet.ModelAndView; |
39 | 52 | ||
40 | -import javax.servlet.http.HttpServletRequest; | ||
41 | -import javax.servlet.http.HttpServletResponse; | ||
42 | -import java.io.IOException; | ||
43 | -import java.util.*; | 53 | +import com.alibaba.fastjson.JSONObject; |
54 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
55 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
56 | + | ||
57 | +import lombok.extern.slf4j.Slf4j; | ||
44 | 58 | ||
45 | /** | 59 | /** |
46 | * <p> | 60 | * <p> |
huaheng-wms-core/src/main/java/org/jeecg/modules/system/controller/SysDictController.java
1 | package org.jeecg.modules.system.controller; | 1 | package org.jeecg.modules.system.controller; |
2 | 2 | ||
3 | -import com.alibaba.fastjson.JSON; | ||
4 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
5 | -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
6 | -import com.baomidou.mybatisplus.core.metadata.IPage; | ||
7 | -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
8 | -import lombok.extern.slf4j.Slf4j; | 3 | +import java.util.ArrayList; |
4 | +import java.util.Arrays; | ||
5 | +import java.util.Date; | ||
6 | +import java.util.HashMap; | ||
7 | +import java.util.List; | ||
8 | +import java.util.Map; | ||
9 | +import java.util.Set; | ||
10 | + | ||
11 | +import javax.servlet.http.HttpServletRequest; | ||
12 | +import javax.servlet.http.HttpServletResponse; | ||
13 | + | ||
9 | import org.apache.shiro.SecurityUtils; | 14 | import org.apache.shiro.SecurityUtils; |
10 | import org.apache.shiro.authz.annotation.RequiresPermissions; | 15 | import org.apache.shiro.authz.annotation.RequiresPermissions; |
11 | -import org.apache.shiro.authz.annotation.RequiresRoles; | ||
12 | import org.jeecg.common.api.vo.Result; | 16 | import org.jeecg.common.api.vo.Result; |
13 | import org.jeecg.common.constant.CacheConstant; | 17 | import org.jeecg.common.constant.CacheConstant; |
14 | import org.jeecg.common.constant.CommonConstant; | 18 | import org.jeecg.common.constant.CommonConstant; |
@@ -36,14 +40,24 @@ import org.springframework.beans.BeanUtils; | @@ -36,14 +40,24 @@ import org.springframework.beans.BeanUtils; | ||
36 | import org.springframework.beans.factory.annotation.Autowired; | 40 | import org.springframework.beans.factory.annotation.Autowired; |
37 | import org.springframework.cache.annotation.CacheEvict; | 41 | import org.springframework.cache.annotation.CacheEvict; |
38 | import org.springframework.data.redis.core.RedisTemplate; | 42 | import org.springframework.data.redis.core.RedisTemplate; |
39 | -import org.springframework.web.bind.annotation.*; | 43 | +import org.springframework.web.bind.annotation.GetMapping; |
44 | +import org.springframework.web.bind.annotation.PathVariable; | ||
45 | +import org.springframework.web.bind.annotation.RequestBody; | ||
46 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
47 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
48 | +import org.springframework.web.bind.annotation.RequestParam; | ||
49 | +import org.springframework.web.bind.annotation.RestController; | ||
40 | import org.springframework.web.multipart.MultipartFile; | 50 | import org.springframework.web.multipart.MultipartFile; |
41 | import org.springframework.web.multipart.MultipartHttpServletRequest; | 51 | import org.springframework.web.multipart.MultipartHttpServletRequest; |
42 | import org.springframework.web.servlet.ModelAndView; | 52 | import org.springframework.web.servlet.ModelAndView; |
43 | 53 | ||
44 | -import javax.servlet.http.HttpServletRequest; | ||
45 | -import javax.servlet.http.HttpServletResponse; | ||
46 | -import java.util.*; | 54 | +import com.alibaba.fastjson.JSON; |
55 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
56 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
57 | +import com.baomidou.mybatisplus.core.metadata.IPage; | ||
58 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
59 | + | ||
60 | +import lombok.extern.slf4j.Slf4j; | ||
47 | 61 | ||
48 | /** | 62 | /** |
49 | * <p> | 63 | * <p> |
huaheng-wms-core/src/main/java/org/jeecg/modules/system/service/impl/SysGatewayRouteServiceImpl.java
1 | package org.jeecg.modules.system.service.impl; | 1 | package org.jeecg.modules.system.service.impl; |
2 | 2 | ||
3 | -import cn.hutool.core.util.ObjectUtil; | ||
4 | -import com.alibaba.fastjson.JSON; | ||
5 | -import com.alibaba.fastjson.JSONObject; | ||
6 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
7 | -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
8 | -import lombok.extern.slf4j.Slf4j; | 3 | +import java.util.List; |
4 | + | ||
9 | import org.jeecg.common.base.BaseMap; | 5 | import org.jeecg.common.base.BaseMap; |
10 | import org.jeecg.common.constant.CacheConstant; | 6 | import org.jeecg.common.constant.CacheConstant; |
11 | import org.jeecg.common.constant.GlobalConstants; | 7 | import org.jeecg.common.constant.GlobalConstants; |
@@ -18,9 +14,13 @@ import org.springframework.data.redis.core.RedisTemplate; | @@ -18,9 +14,13 @@ import org.springframework.data.redis.core.RedisTemplate; | ||
18 | import org.springframework.stereotype.Service; | 14 | import org.springframework.stereotype.Service; |
19 | import org.springframework.transaction.annotation.Transactional; | 15 | import org.springframework.transaction.annotation.Transactional; |
20 | 16 | ||
21 | -import java.util.HashMap; | ||
22 | -import java.util.List; | ||
23 | -import java.util.Map; | 17 | +import com.alibaba.fastjson.JSON; |
18 | +import com.alibaba.fastjson.JSONObject; | ||
19 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
20 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||
21 | + | ||
22 | +import cn.hutool.core.util.ObjectUtil; | ||
23 | +import lombok.extern.slf4j.Slf4j; | ||
24 | 24 | ||
25 | /** | 25 | /** |
26 | * @Description: gateway路由管理 | 26 | * @Description: gateway路由管理 |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java
@@ -12,7 +12,9 @@ import org.jeecg.modules.system.service.ISysDataLogService; | @@ -12,7 +12,9 @@ import org.jeecg.modules.system.service.ISysDataLogService; | ||
12 | import org.jeecg.modules.wms.receipt.receiptContainerHeader.entity.ReceiptContainerDetail; | 12 | import org.jeecg.modules.wms.receipt.receiptContainerHeader.entity.ReceiptContainerDetail; |
13 | import org.jeecg.utils.HuahengRedisUtil; | 13 | import org.jeecg.utils.HuahengRedisUtil; |
14 | import org.jeecg.utils.config.ApplicationConfig; | 14 | import org.jeecg.utils.config.ApplicationConfig; |
15 | +import org.jeecg.utils.interceptor.AccessLimit; | ||
15 | import org.springframework.beans.factory.annotation.Autowired; | 16 | import org.springframework.beans.factory.annotation.Autowired; |
17 | +import org.springframework.web.bind.annotation.GetMapping; | ||
16 | import org.springframework.web.bind.annotation.PostMapping; | 18 | import org.springframework.web.bind.annotation.PostMapping; |
17 | import org.springframework.web.bind.annotation.RequestBody; | 19 | import org.springframework.web.bind.annotation.RequestBody; |
18 | import org.springframework.web.bind.annotation.RequestMapping; | 20 | import org.springframework.web.bind.annotation.RequestMapping; |
@@ -20,6 +22,7 @@ import org.springframework.web.bind.annotation.ResponseBody; | @@ -20,6 +22,7 @@ import org.springframework.web.bind.annotation.ResponseBody; | ||
20 | import org.springframework.web.bind.annotation.RestController; | 22 | import org.springframework.web.bind.annotation.RestController; |
21 | 23 | ||
22 | import com.alibaba.fastjson.JSON; | 24 | import com.alibaba.fastjson.JSON; |
25 | +import com.google.common.util.concurrent.RateLimiter; | ||
23 | 26 | ||
24 | import cn.monitor4all.logRecord.context.LogRecordContext; | 27 | import cn.monitor4all.logRecord.context.LogRecordContext; |
25 | import lombok.extern.slf4j.Slf4j; | 28 | import lombok.extern.slf4j.Slf4j; |
@@ -39,10 +42,15 @@ public class TestController extends HuahengBaseController { | @@ -39,10 +42,15 @@ public class TestController extends HuahengBaseController { | ||
39 | 42 | ||
40 | @Autowired | 43 | @Autowired |
41 | private ISysDataLogService sysDataLogService; | 44 | private ISysDataLogService sysDataLogService; |
42 | - | 45 | + |
43 | @Autowired | 46 | @Autowired |
44 | private ApplicationConfig applicationConfig; | 47 | private ApplicationConfig applicationConfig; |
45 | - | 48 | + |
49 | +// @AccessLimit(seconds = 1, maxCount = 10) | ||
50 | + @PostMapping(value = "/testLimiter") | ||
51 | + public Result<?> testLimiter(@RequestBody Map<String, String> paramMap) { | ||
52 | + return Result.OK(); | ||
53 | + } | ||
46 | 54 | ||
47 | // @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") | 55 | // @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") |
48 | // @ResponseBody | 56 | // @ResponseBody |
@@ -69,9 +77,7 @@ public class TestController extends HuahengBaseController { | @@ -69,9 +77,7 @@ public class TestController extends HuahengBaseController { | ||
69 | LogRecordContext.putVariable("locationCode", "100001");// 操作日志收集 | 77 | LogRecordContext.putVariable("locationCode", "100001");// 操作日志收集 |
70 | LogRecordContext.putVariable("receiptContainerDetailList", receiptContainerDetailList);// 操作日志收集 | 78 | LogRecordContext.putVariable("receiptContainerDetailList", receiptContainerDetailList);// 操作日志收集 |
71 | LogRecordContext.putVariable("extraJsonString", JSON.toJSONString(receiptContainerDetailList));// 操作日志收集 | 79 | LogRecordContext.putVariable("extraJsonString", JSON.toJSONString(receiptContainerDetailList));// 操作日志收集 |
72 | - | ||
73 | - | ||
74 | - | 80 | + |
75 | // String testString = "我是testString1"; | 81 | // String testString = "我是testString1"; |
76 | // LogRecordContext.putVariable("testString1", testString); | 82 | // LogRecordContext.putVariable("testString1", testString); |
77 | // testString = "我是testString2"; | 83 | // testString = "我是testString2"; |
@@ -146,7 +152,7 @@ public class TestController extends HuahengBaseController { | @@ -146,7 +152,7 @@ public class TestController extends HuahengBaseController { | ||
146 | // LogRecordContext.putVariable("result", result); | 152 | // LogRecordContext.putVariable("result", result); |
147 | // return result; | 153 | // return result; |
148 | } | 154 | } |
149 | - | 155 | + |
150 | @ResponseBody | 156 | @ResponseBody |
151 | @PostMapping(value = "/deleteRedisKey") | 157 | @PostMapping(value = "/deleteRedisKey") |
152 | public Result<?> deleteRedisKey(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws InterruptedException { | 158 | public Result<?> deleteRedisKey(@RequestBody Map<String, String> paramMap, HttpServletRequest request) throws InterruptedException { |
huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengRedisUtil.java
1 | package org.jeecg.utils; | 1 | package org.jeecg.utils; |
2 | 2 | ||
3 | -import java.util.List; | ||
4 | -import java.util.Map; | ||
5 | import java.util.concurrent.TimeUnit; | 3 | import java.util.concurrent.TimeUnit; |
6 | 4 | ||
7 | import javax.annotation.Nonnull; | 5 | import javax.annotation.Nonnull; |
6 | +import javax.annotation.Resource; | ||
8 | 7 | ||
9 | -import org.springframework.beans.factory.annotation.Autowired; | ||
10 | -import org.springframework.data.redis.core.StringRedisTemplate; | 8 | +import org.springframework.data.redis.core.RedisTemplate; |
11 | import org.springframework.stereotype.Component; | 9 | import org.springframework.stereotype.Component; |
12 | import org.springframework.util.StringUtils; | 10 | import org.springframework.util.StringUtils; |
13 | 11 | ||
@@ -25,8 +23,8 @@ import lombok.extern.slf4j.Slf4j; | @@ -25,8 +23,8 @@ import lombok.extern.slf4j.Slf4j; | ||
25 | @Component | 23 | @Component |
26 | public class HuahengRedisUtil { | 24 | public class HuahengRedisUtil { |
27 | 25 | ||
28 | - @Autowired | ||
29 | - private StringRedisTemplate redisTemplate; | 26 | + @Resource |
27 | + private RedisTemplate<String, String> redisTemplate; | ||
30 | 28 | ||
31 | private static String toJson(Object obj) { | 29 | private static String toJson(Object obj) { |
32 | if (obj == null) { | 30 | if (obj == null) { |
@@ -99,7 +97,7 @@ public class HuahengRedisUtil { | @@ -99,7 +97,7 @@ public class HuahengRedisUtil { | ||
99 | log.error("操作缓存异常:key:{}", key, e); | 97 | log.error("操作缓存异常:key:{}", key, e); |
100 | } | 98 | } |
101 | } | 99 | } |
102 | - | 100 | + |
103 | /** | 101 | /** |
104 | * 设置key,value 无过期时间 | 102 | * 设置key,value 无过期时间 |
105 | * @author TanYibin | 103 | * @author TanYibin |
@@ -288,4 +286,5 @@ public class HuahengRedisUtil { | @@ -288,4 +286,5 @@ public class HuahengRedisUtil { | ||
288 | } | 286 | } |
289 | return false; | 287 | return false; |
290 | } | 288 | } |
289 | + | ||
291 | } | 290 | } |
292 | \ No newline at end of file | 291 | \ No newline at end of file |
huaheng-wms-core/src/main/java/org/jeecg/utils/config/InterceptorAdapterConfig.java
0 → 100644
1 | +package org.jeecg.utils.config; | ||
2 | + | ||
3 | +import org.jeecg.utils.interceptor.AccessLimitInterceptor; | ||
4 | +import org.springframework.beans.factory.annotation.Autowired; | ||
5 | +import org.springframework.context.annotation.Configuration; | ||
6 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
7 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | ||
8 | + | ||
9 | +@Configuration | ||
10 | +public class InterceptorAdapterConfig extends WebMvcConfigurationSupport { | ||
11 | + @Autowired | ||
12 | + private AccessLimitInterceptor accessLimitInterceptor; | ||
13 | + | ||
14 | + @Override | ||
15 | + public void addInterceptors(InterceptorRegistry registry) { | ||
16 | + // 注册自己的拦截器并设置拦截的请求路径 | ||
17 | + registry.addInterceptor(accessLimitInterceptor).addPathPatterns("/**"); | ||
18 | + super.addInterceptors(registry); | ||
19 | + } | ||
20 | +} | ||
0 | \ No newline at end of file | 21 | \ No newline at end of file |
huaheng-wms-core/src/main/java/org/jeecg/utils/interceptor/AccessLimit.java
0 → 100644
1 | +package org.jeecg.utils.interceptor; | ||
2 | + | ||
3 | +import java.lang.annotation.ElementType; | ||
4 | +import java.lang.annotation.Retention; | ||
5 | +import java.lang.annotation.RetentionPolicy; | ||
6 | +import java.lang.annotation.Target; | ||
7 | + | ||
8 | +@Target(value = {ElementType.METHOD}) | ||
9 | +@Retention(RetentionPolicy.RUNTIME) | ||
10 | +public @interface AccessLimit { | ||
11 | + | ||
12 | + // 单位时间 | ||
13 | + int seconds(); | ||
14 | + | ||
15 | + // 最大次数 | ||
16 | + int maxCount(); | ||
17 | +} | ||
0 | \ No newline at end of file | 18 | \ No newline at end of file |
huaheng-wms-core/src/main/java/org/jeecg/utils/interceptor/AccessLimitInterceptor.java
0 → 100644
1 | +package org.jeecg.utils.interceptor; | ||
2 | + | ||
3 | +import java.util.HashMap; | ||
4 | +import java.util.Iterator; | ||
5 | +import java.util.Map; | ||
6 | +import java.util.Set; | ||
7 | + | ||
8 | +import javax.servlet.http.HttpServletRequest; | ||
9 | +import javax.servlet.http.HttpServletResponse; | ||
10 | + | ||
11 | +import org.jeecg.common.exception.JeecgBootException; | ||
12 | +import org.jeecg.utils.HuahengRedisUtil; | ||
13 | +import org.springframework.beans.factory.annotation.Autowired; | ||
14 | +import org.springframework.stereotype.Component; | ||
15 | +import org.springframework.web.method.HandlerMethod; | ||
16 | +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
17 | + | ||
18 | +import com.alibaba.fastjson.JSON; | ||
19 | + | ||
20 | +import cn.hutool.extra.servlet.ServletUtil; | ||
21 | +import io.micrometer.core.instrument.util.JsonUtils; | ||
22 | +import lombok.extern.slf4j.Slf4j; | ||
23 | + | ||
24 | +/** | ||
25 | + * 拦截器限流 | ||
26 | + * @author TanYibin | ||
27 | + * @createDate 2023年6月12日 | ||
28 | + */ | ||
29 | + | ||
30 | +@Slf4j | ||
31 | +@Component | ||
32 | +public class AccessLimitInterceptor extends HandlerInterceptorAdapter { | ||
33 | + | ||
34 | + @Autowired | ||
35 | + private HuahengRedisUtil huahengRedisUtil; | ||
36 | + | ||
37 | + @Override | ||
38 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
39 | + if (handler instanceof HandlerMethod) { | ||
40 | + HandlerMethod hm = (HandlerMethod)handler; | ||
41 | + // 获取方法中的注解,看是否有该注解 | ||
42 | + AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); | ||
43 | + if (accessLimit == null) { | ||
44 | + return true; | ||
45 | + } | ||
46 | + int seconds = accessLimit.seconds(); | ||
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("超出访问频次限制"); | ||
60 | + } | ||
61 | + } | ||
62 | + return true; | ||
63 | + } | ||
64 | +} | ||
0 | \ No newline at end of file | 65 | \ No newline at end of file |