加密问题
This commit is contained in:
@ -186,6 +186,12 @@ api-decrypt:
|
|||||||
# 请求解密私钥 非对称算法的公私钥 如:SM2,RSA 使用者请自行更换
|
# 请求解密私钥 非对称算法的公私钥 如:SM2,RSA 使用者请自行更换
|
||||||
# 对应前端加密公钥 MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==
|
# 对应前端加密公钥 MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==
|
||||||
privateKey: MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKNPuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gAkM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWowcSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99EcvDQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthhYhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3UP8iWi1Qw0Y=
|
privateKey: MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKNPuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gAkM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWowcSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99EcvDQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthhYhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3UP8iWi1Qw0Y=
|
||||||
|
# cory 放行部分接口
|
||||||
|
excluded-paths:
|
||||||
|
- /doc.html # 放行Swagger文档
|
||||||
|
- /swagger-ui/** # 放行Swagger文档
|
||||||
|
- /v3/api-docs/** # 放行OpenAPI文档
|
||||||
|
- /actuator/** # 放行监控接口
|
||||||
|
|
||||||
springdoc:
|
springdoc:
|
||||||
api-docs:
|
api-docs:
|
||||||
|
@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.util.PathMatcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* api 解密自动配置
|
* api 解密自动配置
|
||||||
@ -20,10 +21,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
public class ApiDecryptAutoConfiguration {
|
public class ApiDecryptAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration(ApiDecryptProperties properties) {
|
public FilterRegistrationBean<CryptoFilter> cryptoFilterRegistration(ApiDecryptProperties properties, PathMatcher pathMatcher) {
|
||||||
FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>();
|
FilterRegistrationBean<CryptoFilter> registration = new FilterRegistrationBean<>();
|
||||||
registration.setDispatcherTypes(DispatcherType.REQUEST);
|
registration.setDispatcherTypes(DispatcherType.REQUEST);
|
||||||
registration.setFilter(new CryptoFilter(properties));
|
registration.setFilter(new CryptoFilter(properties,pathMatcher));
|
||||||
registration.addUrlPatterns("/*");
|
registration.addUrlPatterns("/*");
|
||||||
registration.setName("cryptoFilter");
|
registration.setName("cryptoFilter");
|
||||||
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
|
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
|
||||||
|
@ -11,12 +11,15 @@ import org.dromara.common.core.utils.StringUtils;
|
|||||||
import org.dromara.common.encrypt.annotation.ApiEncrypt;
|
import org.dromara.common.encrypt.annotation.ApiEncrypt;
|
||||||
import org.dromara.common.encrypt.properties.ApiDecryptProperties;
|
import org.dromara.common.encrypt.properties.ApiDecryptProperties;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.PathMatcher;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,24 +29,37 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class CryptoFilter implements Filter {
|
public class CryptoFilter implements Filter {
|
||||||
private final ApiDecryptProperties properties;
|
private final ApiDecryptProperties properties;
|
||||||
|
private final PathMatcher pathMatcher;
|
||||||
|
|
||||||
public CryptoFilter(ApiDecryptProperties properties) {
|
public CryptoFilter(ApiDecryptProperties properties, PathMatcher pathMatcher) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
this.pathMatcher = pathMatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
HttpServletRequest servletRequest = (HttpServletRequest) request;
|
HttpServletRequest servletRequest = (HttpServletRequest) request;
|
||||||
HttpServletResponse servletResponse = (HttpServletResponse) response;
|
HttpServletResponse servletResponse = (HttpServletResponse) response;
|
||||||
boolean responseFlag = true;
|
|
||||||
if ("OPTIONS".equalsIgnoreCase(servletRequest.getMethod())) {
|
// 获取加密注解
|
||||||
// 是预检请求就直接返回不走响应加密
|
|
||||||
responseFlag = false;
|
|
||||||
}
|
|
||||||
// // 获取加密注解
|
|
||||||
//ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest);
|
//ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest);
|
||||||
//boolean responseFlag = apiEncrypt != null && apiEncrypt.response();
|
//boolean responseFlag = apiEncrypt != null && apiEncrypt.response();
|
||||||
|
|
||||||
|
//直接全局加密
|
||||||
|
boolean responseFlag = true;
|
||||||
|
|
||||||
|
// 检查请求是否被排除
|
||||||
|
if (isExcludedPath(servletRequest.getRequestURI())) {
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ("OPTIONS".equalsIgnoreCase(servletRequest.getMethod())) {
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ServletRequest requestWrapper = null;
|
ServletRequest requestWrapper = null;
|
||||||
ServletResponse responseWrapper = null;
|
ServletResponse responseWrapper = null;
|
||||||
EncryptResponseBodyWrapper responseBodyWrapper = null;
|
EncryptResponseBodyWrapper responseBodyWrapper = null;
|
||||||
@ -88,6 +104,25 @@ public class CryptoFilter implements Filter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isExcludedPath(String requestUri) {
|
||||||
|
// 从配置中获取放行的URL列表
|
||||||
|
List<String> excludedPaths = properties.getExcludedPaths();
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(excludedPaths)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查请求URI是否匹配任何一个放行路径
|
||||||
|
for (String excludedPath : excludedPaths) {
|
||||||
|
// 支持简单的Ant风格路径匹配(如 /api/public/**)
|
||||||
|
if (pathMatcher.match(excludedPath, requestUri)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 ApiEncrypt 注解
|
* 获取 ApiEncrypt 注解
|
||||||
*/
|
*/
|
||||||
|
@ -3,6 +3,9 @@ package org.dromara.common.encrypt.properties;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* api解密属性配置类
|
* api解密属性配置类
|
||||||
* @author wdhcr
|
* @author wdhcr
|
||||||
@ -31,4 +34,10 @@ public class ApiDecryptProperties {
|
|||||||
*/
|
*/
|
||||||
private String privateKey;
|
private String privateKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cory
|
||||||
|
* 需要放行的接口路径列表(支持Ant风格路径匹配)
|
||||||
|
*/
|
||||||
|
private List<String> excludedPaths = new ArrayList<>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user