|
|
@@ -5,18 +5,20 @@ import org.springframework.core.MethodParameter;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpInputMessage;
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
+import org.springframework.util.AntPathMatcher;
|
|
|
import org.springframework.util.StreamUtils;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
|
|
|
-import shop.alien.util.encryption.Decrypt;
|
|
|
import shop.alien.util.encryption.StandardAesUtil;
|
|
|
import shop.alien.util.encryption.properties.EncryptProperties;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 请求体解密 Advice
|
|
|
@@ -28,12 +30,31 @@ public class DecryptRequestBodyAdvice extends RequestBodyAdviceAdapter {
|
|
|
@Autowired
|
|
|
private EncryptProperties encryptProperties;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private HttpServletRequest request;
|
|
|
+
|
|
|
+ private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
|
|
|
+
|
|
|
@Override
|
|
|
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
- // 只有开启了配置,且方法或类上有 @Decrypt 注解时才拦截
|
|
|
- return encryptProperties.isEnabled() &&
|
|
|
- (methodParameter.hasMethodAnnotation(Decrypt.class) ||
|
|
|
- methodParameter.getContainingClass().isAnnotationPresent(Decrypt.class));
|
|
|
+ // 1. 如果接口加密关闭,则不解密
|
|
|
+ if (!encryptProperties.isApiEnabled()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 校验路径排除
|
|
|
+ String uri = request.getRequestURI();
|
|
|
+ List<String> excludePaths = encryptProperties.getExcludePaths();
|
|
|
+ if (excludePaths != null && !excludePaths.isEmpty()) {
|
|
|
+ for (String pattern : excludePaths) {
|
|
|
+ if (PATH_MATCHER.match(pattern, uri)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 默认全开启
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -41,6 +62,23 @@ public class DecryptRequestBodyAdvice extends RequestBodyAdviceAdapter {
|
|
|
// 读取加密的请求体
|
|
|
byte[] bodyBytes = StreamUtils.copyToByteArray(inputMessage.getBody());
|
|
|
String encryptedData = new String(bodyBytes, StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ // 核心优化:判断是否为明文 JSON。如果是明文 JSON,则直接跳过解密
|
|
|
+ String trimmedData = encryptedData.trim();
|
|
|
+ if ((trimmedData.startsWith("{") && (trimmedData.contains("\":") || trimmedData.endsWith("}"))) ||
|
|
|
+ (trimmedData.startsWith("[") && (trimmedData.contains("\":") || trimmedData.endsWith("]")))) {
|
|
|
+ return new HttpInputMessage() {
|
|
|
+ @Override
|
|
|
+ public InputStream getBody() throws IOException {
|
|
|
+ return new ByteArrayInputStream(bodyBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpHeaders getHeaders() {
|
|
|
+ return inputMessage.getHeaders();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
// 如果是 JSON 字符串格式(带有双引号),先去掉前后的双引号
|
|
|
if (encryptedData.startsWith("\"") && encryptedData.endsWith("\"")) {
|