接口加密,增加食材营养字段,修复食材报错
This commit is contained in:
1 parent
d60ce3d8ef
commit
8ffbf70ddb
86 files changed
+468
-63
No files matched your search
+5
-3
@@ -1,9 +1,7 @@
|
||||
package com.bsd.pjcloud.common.api.encrypt.core;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.bsd.pjcloud.common.api.encrypt.annotation.decrypt.ApiDecrypt;
|
||||
import com.bsd.pjcloud.common.api.encrypt.bean.CryptoInfoBean;
|
||||
import com.bsd.pjcloud.common.api.encrypt.bean.DecryptHttpInputMessage;
|
||||
import com.bsd.pjcloud.common.api.encrypt.config.ApiEncryptProperties;
|
||||
@@ -51,7 +49,7 @@ public class ApiDecryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Type targetType,
|
||||
Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return AnnotationUtil.hasAnnotation(methodParameter.getMethod(), ApiDecrypt.class);
|
||||
return ApiCryptoUtil.supportsDecrypt(methodParameter);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,6 +67,10 @@ public class ApiDecryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
if (!ApiCryptoUtil.hasEncFlag(ApiCryptoUtil.currentRequest())) {
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
CryptoInfoBean cryptoInfoBean = ApiCryptoUtil.getDecryptInfo(parameter);
|
||||
if (cryptoInfoBean == null) {
|
||||
throw new DecryptBodyFailException("获取解密注解配置为空");
|
||||
|
||||
+5
-3
@@ -1,10 +1,8 @@
|
||||
package com.bsd.pjcloud.common.api.encrypt.core;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.bsd.pjcloud.common.api.encrypt.annotation.encrypt.ApiEncrypt;
|
||||
import com.bsd.pjcloud.common.api.encrypt.bean.CryptoInfoBean;
|
||||
import com.bsd.pjcloud.common.api.encrypt.config.ApiEncryptProperties;
|
||||
import com.bsd.pjcloud.common.api.encrypt.exception.EncryptBodyFailException;
|
||||
@@ -48,7 +46,7 @@ public class ApiEncryptResponseBodyAdvice implements ResponseBodyAdvice<Object>
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class converterType) {
|
||||
return AnnotationUtil.hasAnnotation(returnType.getMethod(), ApiEncrypt.class);
|
||||
return ApiCryptoUtil.supportsEncrypt(returnType);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -59,6 +57,10 @@ public class ApiEncryptResponseBodyAdvice implements ResponseBodyAdvice<Object>
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!ApiCryptoUtil.hasEncFlag(ApiCryptoUtil.currentRequest())) {
|
||||
return body;
|
||||
}
|
||||
|
||||
CryptoInfoBean cryptoInfoBean = ApiCryptoUtil.getEncryptInfo(returnType);
|
||||
if (cryptoInfoBean == null) {
|
||||
throw new EncryptBodyFailException();
|
||||
|
||||
+58
-4
@@ -22,7 +22,10 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -40,6 +43,59 @@ import java.nio.charset.StandardCharsets;
|
||||
@UtilityClass
|
||||
public class ApiCryptoUtil {
|
||||
|
||||
public static final String ENC_FLAG_HEADER = "Enc-Flag";
|
||||
|
||||
/**
|
||||
* 请求头 Enc-Flag 为 true 时执行加解密(与 PC/App 前端约定一致)
|
||||
*/
|
||||
public static boolean hasEncFlag(@Nullable HttpServletRequest request) {
|
||||
if (request == null) {
|
||||
return false;
|
||||
}
|
||||
String encFlag = request.getHeader(ENC_FLAG_HEADER);
|
||||
return StrUtil.isNotBlank(encFlag) && !StrUtil.equalsIgnoreCase(encFlag, "false");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ApiEncrypt findApiEncrypt(MethodParameter methodParameter) {
|
||||
ApiEncrypt encryptBody = AnnotatedElementUtils.findMergedAnnotation(methodParameter.getMethod(),
|
||||
ApiEncrypt.class);
|
||||
if (encryptBody != null) {
|
||||
return encryptBody;
|
||||
}
|
||||
if (methodParameter.getContainingClass() != null) {
|
||||
return AnnotatedElementUtils.findMergedAnnotation(methodParameter.getContainingClass(), ApiEncrypt.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ApiDecrypt findApiDecrypt(MethodParameter methodParameter) {
|
||||
ApiDecrypt decryptBody = AnnotatedElementUtils.findMergedAnnotation(methodParameter.getMethod(),
|
||||
ApiDecrypt.class);
|
||||
if (decryptBody != null) {
|
||||
return decryptBody;
|
||||
}
|
||||
if (methodParameter.getContainingClass() != null) {
|
||||
return AnnotatedElementUtils.findMergedAnnotation(methodParameter.getContainingClass(), ApiDecrypt.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean supportsEncrypt(MethodParameter methodParameter) {
|
||||
return findApiEncrypt(methodParameter) != null;
|
||||
}
|
||||
|
||||
public static boolean supportsDecrypt(MethodParameter methodParameter) {
|
||||
return findApiDecrypt(methodParameter) != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static HttpServletRequest currentRequest() {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return attributes != null ? attributes.getRequest() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取方法控制器上的加密注解信息
|
||||
* @param methodParameter 控制器方法
|
||||
@@ -47,8 +103,7 @@ public class ApiCryptoUtil {
|
||||
*/
|
||||
@Nullable
|
||||
public static CryptoInfoBean getEncryptInfo(MethodParameter methodParameter) {
|
||||
ApiEncrypt encryptBody = AnnotatedElementUtils.findMergedAnnotation(methodParameter.getMethod(),
|
||||
ApiEncrypt.class);
|
||||
ApiEncrypt encryptBody = findApiEncrypt(methodParameter);
|
||||
if (encryptBody == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -62,8 +117,7 @@ public class ApiCryptoUtil {
|
||||
*/
|
||||
@Nullable
|
||||
public static CryptoInfoBean getDecryptInfo(MethodParameter methodParameter) {
|
||||
ApiDecrypt decryptBody = AnnotatedElementUtils.findMergedAnnotation(methodParameter.getMethod(),
|
||||
ApiDecrypt.class);
|
||||
ApiDecrypt decryptBody = findApiDecrypt(methodParameter);
|
||||
if (decryptBody == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user