Skip to content

Commit e9bf92e

Browse files
committed
refactor(encrypt): 拆分字段加密、API 加密模块
1 parent e5002b8 commit e9bf92e

46 files changed

Lines changed: 521 additions & 339 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ continew-starter
160160
│ ├─ continew-starter-data-core(核心模块)
161161
│ ├─ continew-starter-data-mp(MyBatis Plus)
162162
│ └─ continew-starter-data-mf(MyBatis Flex)
163+
├─ continew-starter-encrypt(加密模块)
164+
│ ├─ continew-starter-encrypt-core(核心模块)
165+
│ ├─ continew-starter-encrypt-field(字段加密)
166+
│ └─ continew-starter-encrypt-api(API 加密)
163167
├─ continew-starter-security(安全模块)
164-
│ ├─ continew-starter-security-crypto(加密:字段加解密)
165168
│ ├─ continew-starter-security-mask(脱敏:JSON 数据脱敏)
166169
│ ├─ continew-starter-security-xss(XSS 过滤)
167170
│ └─ continew-starter-security-sensitivewords(敏感词)

continew-starter-bom/pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,25 @@
104104
<version>${revision}</version>
105105
</dependency>
106106

107-
<!-- 安全模块 - 加密 -->
107+
<!-- 加密模块 - 字段加密 -->
108108
<dependency>
109109
<groupId>top.continew.starter</groupId>
110-
<artifactId>continew-starter-security-crypto</artifactId>
110+
<artifactId>continew-starter-encrypt-field</artifactId>
111111
<version>${revision}</version>
112112
</dependency>
113+
<!-- 加密模块 - API 加密 -->
114+
<dependency>
115+
<groupId>top.continew.starter</groupId>
116+
<artifactId>continew-starter-encrypt-api</artifactId>
117+
<version>${revision}</version>
118+
</dependency>
119+
<!-- 加密模块 - 核心模块 -->
120+
<dependency>
121+
<groupId>top.continew.starter</groupId>
122+
<artifactId>continew-starter-encrypt-core</artifactId>
123+
<version>${revision}</version>
124+
</dependency>
125+
113126
<!-- 安全模块 - 脱敏 -->
114127
<dependency>
115128
<groupId>top.continew.starter</groupId>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class OrderedConstants {
3232
public static final class Filter {
3333

3434
/**
35-
* API加/密过滤器顺序
35+
* API 加密过滤器顺序
3636
*/
37-
public static final int API_CRYPTO_FILTER = Ordered.HIGHEST_PRECEDENCE;
37+
public static final int API_ENCRYPT_FILTER = Ordered.HIGHEST_PRECEDENCE;
3838

3939
/**
4040
* 链路追踪过滤器顺序

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,40 @@ public class PropertiesConstants {
5050
public static final String WEB_RESPONSE = WEB + StringConstants.DOT + "response";
5151

5252
/**
53-
* 安全配置
53+
* 加密配置
5454
*/
55-
public static final String SECURITY = CONTINEW_STARTER + StringConstants.DOT + "security";
55+
public static final String ENCRYPT = CONTINEW_STARTER + StringConstants.DOT + "encrypt";
5656

5757
/**
58-
* 安全-数据加/解密配置
58+
* 加密-密码编码器
5959
*/
60-
public static final String SECURITY_CRYPTO = SECURITY + StringConstants.DOT + "crypto";
60+
public static final String ENCRYPT_PASSWORD_ENCODER = ENCRYPT + StringConstants.DOT + "password-encoder";
6161

6262
/**
63-
* 安全-API加/解密配置
63+
* 加密-字段加密
6464
*/
65-
public static final String SECURITY_API_CRYPTO = SECURITY + StringConstants.DOT + "api-crypto";
65+
public static final String ENCRYPT_FIELD = ENCRYPT + StringConstants.DOT + "field";
6666

6767
/**
68-
* 安全-敏感词配置
68+
* 加密-API 加密
6969
*/
70-
public static final String SECURITY_SENSITIVE_WORDS = SECURITY + StringConstants.DOT + "sensitive-words";
70+
public static final String ENCRYPT_API = ENCRYPT + StringConstants.DOT + "api";
71+
72+
/**
73+
* 安全配置
74+
*/
75+
public static final String SECURITY = CONTINEW_STARTER + StringConstants.DOT + "security";
7176

7277
/**
7378
* 安全-XSS 配置
7479
*/
7580
public static final String SECURITY_XSS = SECURITY + StringConstants.DOT + "xss";
7681

82+
/**
83+
* 安全-敏感词配置
84+
*/
85+
public static final String SECURITY_SENSITIVE_WORDS = SECURITY + StringConstants.DOT + "sensitive-words";
86+
7787
/**
7888
* 限流配置
7989
*/

continew-starter-core/src/main/java/top/continew/starter/core/util/SpringWebUtils.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
import cn.hutool.core.util.ReflectUtil;
2121
import cn.hutool.extra.spring.SpringUtil;
2222
import jakarta.servlet.ServletContext;
23+
import jakarta.servlet.http.HttpServletRequest;
2324
import org.springframework.context.ApplicationContext;
2425
import org.springframework.http.server.PathContainer;
2526
import org.springframework.web.accept.ContentNegotiationManager;
27+
import org.springframework.web.method.HandlerMethod;
28+
import org.springframework.web.servlet.HandlerExecutionChain;
2629
import org.springframework.web.servlet.HandlerMapping;
2730
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
2831
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
32+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
2933
import org.springframework.web.util.UrlPathHelper;
3034
import org.springframework.web.util.pattern.PathPattern;
3135
import org.springframework.web.util.pattern.PathPatternParser;
@@ -135,4 +139,31 @@ public static void registerResourceHandler(Map<String, String> handlerMap) {
135139
.getUrlMap();
136140
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
137141
}
142+
143+
/**
144+
* 获取处理器方法
145+
*
146+
* @param request 请求
147+
* @return 处理器方法
148+
* @since 2.14.0
149+
*/
150+
public static HandlerMethod getHandlerMethod(HttpServletRequest request) {
151+
try {
152+
RequestMappingHandlerMapping handlerMapping = SpringUtil
153+
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
154+
HandlerExecutionChain handlerExecutionChain = handlerMapping.getHandler(request);
155+
// 检查是否存在处理链
156+
if (handlerExecutionChain == null) {
157+
return null;
158+
}
159+
// 获取处理器
160+
Object handler = handlerExecutionChain.getHandler();
161+
if (handler instanceof HandlerMethod handlerMethod) {
162+
return handlerMethod;
163+
}
164+
return null;
165+
} catch (Exception e) {
166+
return null;
167+
}
168+
}
138169
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>top.continew.starter</groupId>
8+
<artifactId>continew-starter-encrypt</artifactId>
9+
<version>${revision}</version>
10+
</parent>
11+
12+
<artifactId>continew-starter-encrypt-api</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<name>${project.artifactId}</name>
16+
<description>ContiNew Starter 加密模块 - API 加密</description>
17+
18+
<dependencies>
19+
<!-- 加密模块 - 核心模块 -->
20+
<dependency>
21+
<groupId>top.continew.starter</groupId>
22+
<artifactId>continew-starter-encrypt-core</artifactId>
23+
</dependency>
24+
</dependencies>
25+
</project>

continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/annotation/ApiEncrypt.java renamed to continew-starter-encrypt/continew-starter-encrypt-api/src/main/java/top/continew/starter/encrypt/api/annotation/ApiEncrypt.java

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

17-
package top.continew.starter.security.crypto.annotation;
17+
package top.continew.starter.encrypt.api.annotation;
1818

1919
import java.lang.annotation.*;
2020

2121
/**
22-
* API加密注解
22+
* API 加密注解
2323
*
2424
* @author lishuyan
2525
* @since 2.14.0
@@ -30,8 +30,7 @@
3030
public @interface ApiEncrypt {
3131

3232
/**
33-
* 默认API响应加密
33+
* 是否加密响应
3434
*/
3535
boolean response() default true;
36-
3736
}

continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/ApiCryptoAutoConfiguration.java renamed to continew-starter-encrypt/continew-starter-encrypt-api/src/main/java/top/continew/starter/encrypt/api/autoconfigure/ApiEncryptAutoConfiguration.java

Lines changed: 18 additions & 20 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.crypto.autoconfigure;
17+
package top.continew.starter.encrypt.api.autoconfigure;
1818

1919
import jakarta.annotation.PostConstruct;
2020
import jakarta.servlet.DispatcherType;
@@ -27,40 +27,38 @@
2727
import org.springframework.context.annotation.Bean;
2828
import top.continew.starter.core.constant.OrderedConstants;
2929
import top.continew.starter.core.constant.PropertiesConstants;
30-
import top.continew.starter.security.crypto.filter.ApiCryptoFilter;
30+
import top.continew.starter.core.constant.StringConstants;
31+
import top.continew.starter.encrypt.api.filter.ApiEncryptFilter;
3132

3233
/**
33-
* API加/解密自动配置
34+
* API 加密自动配置
3435
*
3536
* @author lishuyan
37+
* @author Charles7c
3638
* @since 2.14.0
3739
*/
3840
@AutoConfiguration
39-
@EnableConfigurationProperties(ApiCryptoProperties.class)
40-
@ConditionalOnProperty(prefix = PropertiesConstants.SECURITY_API_CRYPTO, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
41-
public class ApiCryptoAutoConfiguration {
41+
@EnableConfigurationProperties(ApiEncryptProperties.class)
42+
@ConditionalOnProperty(prefix = PropertiesConstants.ENCRYPT_API, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
43+
public class ApiEncryptAutoConfiguration {
4244

43-
private static final Logger log = LoggerFactory.getLogger(ApiCryptoAutoConfiguration.class);
45+
private static final Logger log = LoggerFactory.getLogger(ApiEncryptAutoConfiguration.class);
4446

4547
/**
46-
* API加/解密过滤器
47-
*
48-
* @param properties 配置
49-
* @return 过滤器
48+
* API 加密过滤器
5049
*/
5150
@Bean
52-
public FilterRegistrationBean<ApiCryptoFilter> apiCryptoFilterRegistration(ApiCryptoProperties properties) {
53-
FilterRegistrationBean<ApiCryptoFilter> registration = new FilterRegistrationBean<>();
54-
registration.setDispatcherTypes(DispatcherType.REQUEST);
55-
registration.setFilter(new ApiCryptoFilter(properties));
56-
registration.addUrlPatterns("/*");
57-
registration.setName("apiCryptoFilter");
58-
registration.setOrder(OrderedConstants.Filter.API_CRYPTO_FILTER);
59-
return registration;
51+
public FilterRegistrationBean<ApiEncryptFilter> apiEncryptFilter(ApiEncryptProperties properties) {
52+
FilterRegistrationBean<ApiEncryptFilter> registrationBean = new FilterRegistrationBean<>();
53+
registrationBean.setFilter(new ApiEncryptFilter(properties));
54+
registrationBean.setOrder(OrderedConstants.Filter.API_ENCRYPT_FILTER);
55+
registrationBean.addUrlPatterns(StringConstants.PATH_PATTERN_CURRENT_DIR);
56+
registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
57+
return registrationBean;
6058
}
6159

6260
@PostConstruct
6361
public void postConstruct() {
64-
log.debug("[ContiNew Starter] - Auto Configuration 'Security-API-Crypto' completed initialization.");
62+
log.debug("[ContiNew Starter] - Auto Configuration 'Encrypt-API' completed initialization.");
6563
}
6664
}

continew-starter-security/continew-starter-security-crypto/src/main/java/top/continew/starter/security/crypto/autoconfigure/ApiCryptoProperties.java renamed to continew-starter-encrypt/continew-starter-encrypt-api/src/main/java/top/continew/starter/encrypt/api/autoconfigure/ApiEncryptProperties.java

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

17-
package top.continew.starter.security.crypto.autoconfigure;
17+
package top.continew.starter.encrypt.api.autoconfigure;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
2020
import top.continew.starter.core.constant.PropertiesConstants;
2121

2222
/**
23-
* API加/解密属性配置
23+
* API 加密配置属性
2424
*
2525
* @author lishuyan
2626
* @since 2.14.0
2727
*/
28-
@ConfigurationProperties(PropertiesConstants.SECURITY_API_CRYPTO)
29-
public class ApiCryptoProperties {
28+
@ConfigurationProperties(PropertiesConstants.ENCRYPT_API)
29+
public class ApiEncryptProperties {
3030

3131
/**
3232
* 是否启用
@@ -36,7 +36,7 @@ public class ApiCryptoProperties {
3636
/**
3737
* 请求头中 AES 密钥 键名
3838
*/
39-
private String secretKeyHeader = "X-Api-Crypto";
39+
private String secretKeyHeader = "X-Api-Encrypt";
4040

4141
/**
4242
* 响应加密公钥

0 commit comments

Comments
 (0)