♻️ Refactoring code. #5236 支持 Get请求接口参数 【文档需要更新】

This commit is contained in:
lbw committed 2023-12-03 23:44:09 +08:00
1 parent 2ec42bb871
commit 9df56335f6
6 files changed
+37 -9

No files matched your search

@@ -19,6 +19,12 @@ import java.lang.annotation.*;
@Inherited
public @interface ApiDecrypt {
/**
* 参数名称 仅针对GET请求有效
* @return string
*/
String parameter() default "";
/**
* 解密类型
* @return 类型
@@ -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}
@@ -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}
@@ -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}
@@ -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<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new ApiDecryptParamResolver(apiEncryptProperties, objectMapper));
argumentResolvers.add(new ApiDecryptParamResolver(apiEncryptProperties));
}
}
@@ -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());
}
}