From 9df56335f69c3513899ea1df14315ec781fa28d8 Mon Sep 17 00:00:00 2001 From: lbw Date: Thu, 12 Oct 2023 15:55:09 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20#5236=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Get=E8=AF=B7=E6=B1=82=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20=E3=80=90=E6=96=87=E6=A1=A3=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=9B=B4=E6=96=B0=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/encrypt/annotation/decrypt/ApiDecrypt.java | 6 ++++++ .../encrypt/annotation/decrypt/ApiDecryptAes.java | 7 +++++++ .../encrypt/annotation/decrypt/ApiDecryptDes.java | 7 +++++++ .../encrypt/annotation/decrypt/ApiDecryptSm4.java | 7 +++++++ .../config/ApiEncryptParamConfiguration.java | 5 +---- .../api/encrypt/core/ApiDecryptParamResolver.java | 14 +++++++++----- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecrypt.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecrypt.java index e1d3566ed..a17d3c554 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecrypt.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecrypt.java @@ -19,6 +19,12 @@ import java.lang.annotation.*; @Inherited public @interface ApiDecrypt { + /** + * 参数名称 仅针对GET请求有效 + * @return string + */ + String parameter() default ""; + /** * 解密类型 * @return 类型 diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptAes.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptAes.java index b1f2929d9..2b7be4a8e 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptAes.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptAes.java @@ -19,6 +19,13 @@ import java.lang.annotation.*; @ApiDecrypt(EncryptType.AES) public @interface ApiDecryptAes { + /** + * 参数名称 仅针对GET请求有效 + * @return string + */ + @AliasFor(annotation = ApiDecrypt.class, attribute = "parameter") + String value() default ""; + /** * Alias for {@link ApiDecrypt#secretKey()}. * @return {String} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptDes.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptDes.java index e60dbd7ec..3c133c0d4 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptDes.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptDes.java @@ -16,6 +16,13 @@ import java.lang.annotation.*; @ApiDecrypt(EncryptType.DES) public @interface ApiDecryptDes { + /** + * 参数名称 仅针对GET请求有效 + * @return string + */ + @AliasFor(annotation = ApiDecrypt.class, attribute = "parameter") + String value() default ""; + /** * Alias for {@link ApiDecrypt#secretKey()}. * @return {String} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java index b8caffd41..423e382c3 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/annotation/decrypt/ApiDecryptSm4.java @@ -18,6 +18,13 @@ import java.lang.annotation.*; @ApiDecrypt(EncryptType.SM4) public @interface ApiDecryptSm4 { + /** + * 参数名称 仅针对GET请求有效 + * @return string + */ + @AliasFor(annotation = ApiDecrypt.class, attribute = "parameter") + String value() default ""; + /** * Alias for {@link ApiDecrypt#secretKey()}. * @return {String} diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptParamConfiguration.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptParamConfiguration.java index 6e6cc157c..510ce0176 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptParamConfiguration.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/config/ApiEncryptParamConfiguration.java @@ -16,7 +16,6 @@ package com.pig4cloud.pigx.common.api.encrypt.config; -import com.fasterxml.jackson.databind.ObjectMapper; import com.pig4cloud.pigx.common.api.encrypt.core.ApiDecryptParamResolver; import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -36,10 +35,8 @@ public class ApiEncryptParamConfiguration implements WebMvcConfigurer { private final ApiEncryptProperties apiEncryptProperties; - private final ObjectMapper objectMapper; - public void addArgumentResolvers(List argumentResolvers) { - argumentResolvers.add(new ApiDecryptParamResolver(apiEncryptProperties, objectMapper)); + argumentResolvers.add(new ApiDecryptParamResolver(apiEncryptProperties)); } } diff --git a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/ApiDecryptParamResolver.java b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/ApiDecryptParamResolver.java index b74dd57f0..277c0d436 100644 --- a/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/ApiDecryptParamResolver.java +++ b/pigx-common/pigx-common-encrypt-api/src/main/java/com/pig4cloud/pigx/common/api/encrypt/core/ApiDecryptParamResolver.java @@ -17,7 +17,8 @@ package com.pig4cloud.pigx.common.api.encrypt.core; import cn.hutool.core.util.StrUtil; -import com.fasterxml.jackson.databind.ObjectMapper; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; import com.pig4cloud.pigx.common.api.encrypt.annotation.decrypt.ApiDecrypt; import com.pig4cloud.pigx.common.api.encrypt.bean.CryptoInfoBean; import com.pig4cloud.pigx.common.api.encrypt.config.ApiEncryptProperties; @@ -44,8 +45,6 @@ public class ApiDecryptParamResolver implements HandlerMethodArgumentResolver { private final ApiEncryptProperties properties; - private final ObjectMapper objectMapper; - @Override public boolean supportsParameter(MethodParameter parameter) { return AnnotatedElementUtils.hasAnnotation(parameter.getParameter(), ApiDecrypt.class); @@ -57,6 +56,11 @@ public class ApiDecryptParamResolver implements HandlerMethodArgumentResolver { NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Parameter parameter = methodParameter.getParameter(); ApiDecrypt apiDecrypt = AnnotatedElementUtils.getMergedAnnotation(parameter, ApiDecrypt.class); + + if (StrUtil.isBlank(apiDecrypt.parameter())) { + return null; + } + String text = webRequest.getParameter(properties.getParamName()); if (StrUtil.isBlank(text)) { return null; @@ -64,8 +68,8 @@ public class ApiDecryptParamResolver implements HandlerMethodArgumentResolver { CryptoInfoBean infoBean = new CryptoInfoBean(apiDecrypt.value(), apiDecrypt.secretKey()); byte[] textBytes = text.getBytes(StandardCharsets.UTF_8); byte[] decryptData = ApiCryptoUtil.decryptData(textBytes, infoBean); - - return objectMapper.readValue(decryptData, parameter.getType()); + JSONObject jsonObject = JSONUtil.parseObj(decryptData); + return jsonObject.get(apiDecrypt.parameter(), parameter.getType()); } }