接口解密中的 fastjson 解析对象统一改为使用 jackson

This commit is contained in:
zhoumingfa 2025-03-28 21:12:59 +08:00
parent a6607c063b
commit 5fb1456080
4 changed files with 26 additions and 24 deletions

View File

@ -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;
@ -37,6 +37,9 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
@Resource
private ApiEncryptService apiEncryptService;
@Resource
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class);
@ -46,7 +49,7 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> 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;
}

View File

@ -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;
@ -30,7 +29,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@Slf4j
@ControllerAdvice
public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> {
public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO<Object>> {
@Resource
private ApiEncryptService apiEncryptService;
@ -44,19 +43,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> {
}
@Override
public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body.getData() == null) {
public ResponseDTO<Object> beforeBodyWrite(ResponseDTO<Object> body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> 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;
}
}

View File

@ -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,6 +37,9 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
@Resource
private ApiEncryptService apiEncryptService;
@Resource
private ObjectMapper objectMapper;
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class);
@ -46,7 +49,7 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> 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;
}

View File

@ -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<ResponseDTO> {
public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO<Object>> {
@Resource
private ApiEncryptService apiEncryptService;
@ -45,19 +44,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> {
}
@Override
public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body.getData() == null) {
public ResponseDTO<Object> beforeBodyWrite(ResponseDTO<Object> body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> 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;
}
}