diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java index 9adf42ad..72a86e29 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java @@ -1,6 +1,6 @@ package net.lab1024.sa.base.module.support.apiencrypt.advice; -import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import net.lab1024.sa.base.common.util.SmartStringUtil; @@ -25,7 +25,7 @@ import java.lang.reflect.Type; * @Date 2023/10/21 11:41:46 * @Wechat zhuoda1024 * @Email lab1024@163.com - * @Copyright 1024创新实验室,Since 2012 + * @Copyright 1024创新实验室,Since 2012 */ @Slf4j @@ -37,16 +37,19 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter { @Resource private ApiEncryptService apiEncryptService; + @Resource + private ObjectMapper objectMapper; + @Override public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class); } @Override - public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { + public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { try { String bodyStr = IOUtils.toString(inputMessage.getBody(), ENCODING); - ApiEncryptForm apiEncryptForm = JSONObject.parseObject(bodyStr, ApiEncryptForm.class); + ApiEncryptForm apiEncryptForm = objectMapper.readValue(bodyStr, ApiEncryptForm.class); if (SmartStringUtil.isEmpty(apiEncryptForm.getEncryptData())) { return inputMessage; } diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java index 0d6afa15..ee90c092 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java @@ -1,6 +1,5 @@ package net.lab1024.sa.base.module.support.apiencrypt.advice; -import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.Resource; @@ -24,13 +23,13 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; * @Date 2023/10/24 09:52:58 * @Wechat zhuoda1024 * @Email lab1024@163.com - * @Copyright 1024创新实验室,Since 2012 + * @Copyright 1024创新实验室,Since 2012 */ @Slf4j @ControllerAdvice -public class EncryptResponseAdvice implements ResponseBodyAdvice { +public class EncryptResponseAdvice implements ResponseBodyAdvice> { @Resource private ApiEncryptService apiEncryptService; @@ -44,19 +43,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice { } @Override - public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { - if (body.getData() == null) { + public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { + if (body == null || body.getData() == null) { return body; } - String encrypt = null; try { - encrypt = apiEncryptService.encrypt(objectMapper.writeValueAsString(body.getData())); + String encrypt = apiEncryptService.encrypt(objectMapper.writeValueAsString(body.getData())); + body.setData(encrypt); + body.setDataType(DataTypeEnum.ENCRYPT.getValue()); } catch (JsonProcessingException e) { throw new RuntimeException(e); } - body.setData(encrypt); - body.setDataType(DataTypeEnum.ENCRYPT.getValue()); return body; } } diff --git a/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java b/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java index ea087e4d..03c0397d 100644 --- a/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java +++ b/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/DecryptRequestAdvice.java @@ -1,6 +1,6 @@ package net.lab1024.sa.base.module.support.apiencrypt.advice; -import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import net.lab1024.sa.base.common.util.SmartStringUtil; import net.lab1024.sa.base.module.support.apiencrypt.annotation.ApiDecrypt; @@ -37,16 +37,19 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter { @Resource private ApiEncryptService apiEncryptService; + @Resource + private ObjectMapper objectMapper; + @Override public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class); } @Override - public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { + public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) { try { String bodyStr = IOUtils.toString(inputMessage.getBody(), ENCODING); - ApiEncryptForm apiEncryptForm = JSONObject.parseObject(bodyStr, ApiEncryptForm.class); + ApiEncryptForm apiEncryptForm = objectMapper.readValue(bodyStr, ApiEncryptForm.class); if (SmartStringUtil.isEmpty(apiEncryptForm.getEncryptData())) { return inputMessage; } diff --git a/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java b/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java index d38544fc..2e9df5fa 100644 --- a/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java +++ b/smart-admin-api-java8-springboot2/sa-base/src/main/java/net/lab1024/sa/base/module/support/apiencrypt/advice/EncryptResponseAdvice.java @@ -1,6 +1,5 @@ package net.lab1024.sa.base.module.support.apiencrypt.advice; -import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; @@ -31,7 +30,7 @@ import javax.annotation.Resource; @Slf4j @ControllerAdvice -public class EncryptResponseAdvice implements ResponseBodyAdvice { +public class EncryptResponseAdvice implements ResponseBodyAdvice> { @Resource private ApiEncryptService apiEncryptService; @@ -45,19 +44,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice { } @Override - public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { - if (body.getData() == null) { + public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { + if (body == null || body.getData() == null) { return body; } - String encrypt = null; try { - encrypt = apiEncryptService.encrypt(objectMapper.writeValueAsString(body.getData())); + String encrypt = apiEncryptService.encrypt(objectMapper.writeValueAsString(body.getData())); + body.setData(encrypt); + body.setDataType(DataTypeEnum.ENCRYPT.getValue()); } catch (JsonProcessingException e) { throw new RuntimeException(e); } - body.setData(encrypt); - body.setDataType(DataTypeEnum.ENCRYPT.getValue()); return body; } }