Skip to content

Commit 2b3de0c

Browse files
committed
refactor(ratelimiter): 将限流相关代码从 security 模块中分离,创建独立的 ratelimiter 模块
修复部分幂等配置错误
1 parent 27a71cf commit 2b3de0c

File tree

20 files changed

+90
-70
lines changed

20 files changed

+90
-70
lines changed

continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public class PropertiesConstants {
4949
*/
5050
public static final String SECURITY_CRYPTO = SECURITY + StringConstants.DOT + "crypto";
5151

52-
/**
53-
* 限流器配置
54-
*/
55-
public static final String SECURITY_LIMITER = SECURITY + StringConstants.DOT + "limiter";
56-
5752
/**
5853
* 敏感词配置
5954
*/
@@ -139,10 +134,15 @@ public class PropertiesConstants {
139134
*/
140135
public static final String TENANT = CONTINEW_STARTER + StringConstants.DOT + "tenant";
141136

137+
/**
138+
* 限流配置
139+
*/
140+
public static final String RATE_LIMITER = CONTINEW_STARTER + StringConstants.DOT + "rate-limiter";
141+
142142
/**
143143
* 幂等配置
144144
*/
145-
public static final String IDEMPOTENT = "idempotent";
145+
public static final String IDEMPOTENT = CONTINEW_STARTER + StringConstants.DOT + "idempotent";
146146

147147
private PropertiesConstants() {
148148
}

continew-starter-dependencies/pom.xml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,20 @@
538538
<version>${revision}</version>
539539
</dependency>
540540

541+
<!-- 幂等模块 -->
542+
<dependency>
543+
<groupId>top.continew</groupId>
544+
<artifactId>continew-starter-idempotent</artifactId>
545+
<version>${revision}</version>
546+
</dependency>
547+
548+
<!-- 限流模块 -->
549+
<dependency>
550+
<groupId>top.continew</groupId>
551+
<artifactId>continew-starter-ratelimiter</artifactId>
552+
<version>${revision}</version>
553+
</dependency>
554+
541555
<!-- 日志模块 - 基于拦截器实现(Spring Boot Actuator HttpTrace 增强版) -->
542556
<dependency>
543557
<groupId>top.continew</groupId>
@@ -566,6 +580,13 @@
566580
<version>${revision}</version>
567581
</dependency>
568582

583+
<!-- 安全模块 - 敏感词 -->
584+
<dependency>
585+
<groupId>top.continew</groupId>
586+
<artifactId>continew-starter-security-sensitivewords</artifactId>
587+
<version>${revision}</version>
588+
</dependency>
589+
569590
<!-- 安全模块 - 加密 -->
570591
<dependency>
571592
<groupId>top.continew</groupId>
@@ -587,20 +608,6 @@
587608
<version>${revision}</version>
588609
</dependency>
589610

590-
<!-- 安全模块 - 限流 -->
591-
<dependency>
592-
<groupId>top.continew</groupId>
593-
<artifactId>continew-starter-security-limiter</artifactId>
594-
<version>${revision}</version>
595-
</dependency>
596-
597-
<!-- 安全模块 - 敏感词 -->
598-
<dependency>
599-
<groupId>top.continew</groupId>
600-
<artifactId>continew-starter-security-sensitivewords</artifactId>
601-
<version>${revision}</version>
602-
</dependency>
603-
604611
<!-- API 文档模块 -->
605612
<dependency>
606613
<groupId>top.continew</groupId>

continew-starter-idempotent/src/main/java/top/continew/starter/idempotent/autoconfigure/IdempotentAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.core.ResolvableType;
29+
import top.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
2930
import top.continew.starter.core.constant.PropertiesConstants;
3031
import top.continew.starter.idempotent.aop.IdempotentAspect;
3132
import top.continew.starter.idempotent.generator.IdempotentNameGenerator;
@@ -37,7 +38,7 @@
3738
* @author Charles7c
3839
* @since 2.10.0
3940
*/
40-
@AutoConfiguration
41+
@AutoConfiguration(after = RedissonAutoConfiguration.class)
4142
@EnableConfigurationProperties(IdempotentProperties.class)
4243
@ConditionalOnProperty(prefix = PropertiesConstants.IDEMPOTENT, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
4344
public class IdempotentAutoConfiguration {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"top.continew.starter.idempotent.annotation.Idempotent@key":{
3+
"method":{
4+
"parameters": true
5+
}
6+
}
7+
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@
33
<modelVersion>4.0.0</modelVersion>
44
<parent>
55
<groupId>top.continew</groupId>
6-
<artifactId>continew-starter-security</artifactId>
6+
<artifactId>continew-starter</artifactId>
77
<version>${revision}</version>
88
</parent>
99

10-
<artifactId>continew-starter-security-limiter</artifactId>
11-
<description>ContiNew Starter 安全模块 - 限流</description>
10+
<artifactId>continew-starter-ratelimiter</artifactId>
11+
<description>ContiNew Starter 限流模块</description>
1212

1313
<dependencies>
14-
<dependency>
15-
<groupId>org.springframework.boot</groupId>
16-
<artifactId>spring-boot-starter-aop</artifactId>
17-
</dependency>
18-
1914
<!-- Web 模块 -->
2015
<dependency>
2116
<groupId>top.continew</groupId>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.limiter.annotation;
17+
package top.continew.starter.ratelimiter.annotation;
1818

19-
import top.continew.starter.security.limiter.enums.LimitType;
19+
import top.continew.starter.ratelimiter.enums.LimitType;
2020

2121
import java.lang.annotation.*;
2222
import java.util.concurrent.TimeUnit;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.limiter.annotation;
17+
package top.continew.starter.ratelimiter.annotation;
1818

1919
import java.lang.annotation.*;
2020

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.limiter.core;
17+
package top.continew.starter.ratelimiter.aop;
1818

1919
import cn.hutool.core.convert.Convert;
2020
import cn.hutool.core.text.CharSequenceUtil;
@@ -27,15 +27,15 @@
2727
import org.aspectj.lang.annotation.Pointcut;
2828
import org.aspectj.lang.reflect.MethodSignature;
2929
import org.redisson.api.*;
30-
import org.springframework.stereotype.Component;
3130
import top.continew.starter.cache.redisson.util.RedisUtils;
3231
import top.continew.starter.core.constant.StringConstants;
3332
import top.continew.starter.core.util.expression.ExpressionUtils;
34-
import top.continew.starter.security.limiter.annotation.RateLimiter;
35-
import top.continew.starter.security.limiter.annotation.RateLimiters;
36-
import top.continew.starter.security.limiter.autoconfigure.RateLimiterProperties;
37-
import top.continew.starter.security.limiter.enums.LimitType;
38-
import top.continew.starter.security.limiter.exception.RateLimiterException;
33+
import top.continew.starter.ratelimiter.annotation.RateLimiter;
34+
import top.continew.starter.ratelimiter.annotation.RateLimiters;
35+
import top.continew.starter.ratelimiter.autoconfigure.RateLimiterProperties;
36+
import top.continew.starter.ratelimiter.generator.RateLimiterNameGenerator;
37+
import top.continew.starter.ratelimiter.enums.LimitType;
38+
import top.continew.starter.ratelimiter.exception.RateLimiterException;
3939
import top.continew.starter.web.util.SpringWebUtils;
4040

4141
import java.lang.reflect.Method;
@@ -51,7 +51,6 @@
5151
* @since 2.2.0
5252
*/
5353
@Aspect
54-
@Component
5554
public class RateLimiterAspect {
5655

5756
private static final ConcurrentHashMap<String, RRateLimiter> RATE_LIMITER_CACHE = new ConcurrentHashMap<>();
@@ -70,14 +69,14 @@ public RateLimiterAspect(RateLimiterProperties properties,
7069
/**
7170
* 单个限流注解切点
7271
*/
73-
@Pointcut("@annotation(top.continew.starter.security.limiter.annotation.RateLimiter)")
72+
@Pointcut("@annotation(top.continew.starter.ratelimiter.annotation.RateLimiter)")
7473
public void rateLimiterPointCut() {
7574
}
7675

7776
/**
7877
* 多个限流注解切点
7978
*/
80-
@Pointcut("@annotation(top.continew.starter.security.limiter.annotation.RateLimiters)")
79+
@Pointcut("@annotation(top.continew.starter.ratelimiter.annotation.RateLimiters)")
8180
public void rateLimitersPointCut() {
8281
}
8382

@@ -144,23 +143,23 @@ private boolean isRateLimited(ProceedingJoinPoint joinPoint, RateLimiter rateLim
144143
}
145144

146145
/**
147-
* 获取限流缓存 Key
146+
* 获取缓存 Key
148147
*
149148
* @param joinPoint 切点
150149
* @param rateLimiter 限流注解
151-
* @return 限流缓存 Key
150+
* @return 缓存 Key
152151
*/
153152
private String getCacheKey(JoinPoint joinPoint, RateLimiter rateLimiter) {
154153
Object target = joinPoint.getTarget();
155154
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
156155
Method method = methodSignature.getMethod();
157156
Object[] args = joinPoint.getArgs();
158-
// 获取限流名称
157+
// 获取名称
159158
String name = rateLimiter.name();
160159
if (CharSequenceUtil.isBlank(name)) {
161160
name = nameGenerator.generate(target, method, args);
162161
}
163-
// 解析限流 Key
162+
// 解析 Key
164163
String key = rateLimiter.key();
165164
if (CharSequenceUtil.isNotBlank(key)) {
166165
Object eval = ExpressionUtils.eval(key, target, method, args);
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.limiter.autoconfigure;
17+
package top.continew.starter.ratelimiter.autoconfigure;
1818

1919
import jakarta.annotation.PostConstruct;
20+
import org.redisson.api.RedissonClient;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223
import org.springframework.boot.autoconfigure.AutoConfiguration;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2526
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2627
import org.springframework.context.annotation.Bean;
27-
import org.springframework.context.annotation.ComponentScan;
28+
import top.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
2829
import top.continew.starter.core.constant.PropertiesConstants;
29-
import top.continew.starter.security.limiter.core.DefaultRateLimiterNameGenerator;
30-
import top.continew.starter.security.limiter.core.RateLimiterNameGenerator;
30+
import top.continew.starter.ratelimiter.aop.RateLimiterAspect;
31+
import top.continew.starter.ratelimiter.generator.DefaultRateLimiterNameGenerator;
32+
import top.continew.starter.ratelimiter.generator.RateLimiterNameGenerator;
3133

3234
/**
3335
* 限流器自动配置
@@ -36,14 +38,23 @@
3638
* @author Charles7c
3739
* @since 2.2.0
3840
*/
39-
@AutoConfiguration
41+
@AutoConfiguration(after = RedissonAutoConfiguration.class)
4042
@EnableConfigurationProperties(RateLimiterProperties.class)
41-
@ComponentScan({"top.continew.starter.security.limiter.core"})
42-
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_LIMITER, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
43+
@ConditionalOnProperty(prefix = PropertiesConstants.RATE_LIMITER, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
4344
public class RateLimiterAutoConfiguration {
4445

4546
private static final Logger log = LoggerFactory.getLogger(RateLimiterAutoConfiguration.class);
4647

48+
/**
49+
* 限流器切面
50+
*/
51+
@Bean
52+
public RateLimiterAspect rateLimiterAspect(RateLimiterProperties properties,
53+
RateLimiterNameGenerator rateLimiterNameGenerator,
54+
RedissonClient redissonClient) {
55+
return new RateLimiterAspect(properties, rateLimiterNameGenerator, redissonClient);
56+
}
57+
4758
/**
4859
* 限流器名称生成器
4960
*/
@@ -55,6 +66,6 @@ public RateLimiterNameGenerator nameGenerator() {
5566

5667
@PostConstruct
5768
public void postConstruct() {
58-
log.debug("[ContiNew Starter] - Auto Configuration 'Security-RateLimiter' completed initialization.");
69+
log.debug("[ContiNew Starter] - Auto Configuration 'RateLimiter' completed initialization.");
5970
}
6071
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.security.limiter.autoconfigure;
17+
package top.continew.starter.ratelimiter.autoconfigure;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
2020
import top.continew.starter.core.constant.PropertiesConstants;
@@ -25,7 +25,7 @@
2525
* @author KAI
2626
* @since 2.2.0
2727
*/
28-
@ConfigurationProperties(PropertiesConstants.SECURITY_LIMITER)
28+
@ConfigurationProperties(PropertiesConstants.RATE_LIMITER)
2929
public class RateLimiterProperties {
3030

3131
/**

0 commit comments

Comments
 (0)