接口解密中的 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; 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 jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.util.SmartStringUtil; import net.lab1024.sa.base.common.util.SmartStringUtil;
@ -25,7 +25,7 @@ import java.lang.reflect.Type;
* @Date 2023/10/21 11:41:46 * @Date 2023/10/21 11:41:46
* @Wechat zhuoda1024 * @Wechat zhuoda1024
* @Email lab1024@163.com * @Email lab1024@163.com
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>Since 2012 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>Since 2012
*/ */
@Slf4j @Slf4j
@ -37,16 +37,19 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
@Resource @Resource
private ApiEncryptService apiEncryptService; private ApiEncryptService apiEncryptService;
@Resource
private ObjectMapper objectMapper;
@Override @Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { 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); return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class);
} }
@Override @Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
try { try {
String bodyStr = IOUtils.toString(inputMessage.getBody(), ENCODING); 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())) { if (SmartStringUtil.isEmpty(apiEncryptForm.getEncryptData())) {
return inputMessage; return inputMessage;
} }

View File

@ -1,6 +1,5 @@
package net.lab1024.sa.base.module.support.apiencrypt.advice; package net.lab1024.sa.base.module.support.apiencrypt.advice;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -24,13 +23,13 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
* @Date 2023/10/24 09:52:58 * @Date 2023/10/24 09:52:58
* @Wechat zhuoda1024 * @Wechat zhuoda1024
* @Email lab1024@163.com * @Email lab1024@163.com
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>Since 2012 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>Since 2012
*/ */
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> { public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO<Object>> {
@Resource @Resource
private ApiEncryptService apiEncryptService; private ApiEncryptService apiEncryptService;
@ -44,19 +43,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> {
} }
@Override @Override
public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { public ResponseDTO<Object> beforeBodyWrite(ResponseDTO<Object> body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body.getData() == null) { if (body == null || body.getData() == null) {
return body; return body;
} }
String encrypt = null;
try { 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) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
body.setData(encrypt);
body.setDataType(DataTypeEnum.ENCRYPT.getValue());
return body; return body;
} }
} }

View File

@ -1,6 +1,6 @@
package net.lab1024.sa.base.module.support.apiencrypt.advice; 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 lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.util.SmartStringUtil; import net.lab1024.sa.base.common.util.SmartStringUtil;
import net.lab1024.sa.base.module.support.apiencrypt.annotation.ApiDecrypt; import net.lab1024.sa.base.module.support.apiencrypt.annotation.ApiDecrypt;
@ -37,16 +37,19 @@ public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
@Resource @Resource
private ApiEncryptService apiEncryptService; private ApiEncryptService apiEncryptService;
@Resource
private ObjectMapper objectMapper;
@Override @Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { 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); return methodParameter.hasMethodAnnotation(ApiDecrypt.class) || methodParameter.hasParameterAnnotation(ApiDecrypt.class) || methodParameter.getContainingClass().isAnnotationPresent(ApiDecrypt.class);
} }
@Override @Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
try { try {
String bodyStr = IOUtils.toString(inputMessage.getBody(), ENCODING); 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())) { if (SmartStringUtil.isEmpty(apiEncryptForm.getEncryptData())) {
return inputMessage; return inputMessage;
} }

View File

@ -1,6 +1,5 @@
package net.lab1024.sa.base.module.support.apiencrypt.advice; package net.lab1024.sa.base.module.support.apiencrypt.advice;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -31,7 +30,7 @@ import javax.annotation.Resource;
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> { public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO<Object>> {
@Resource @Resource
private ApiEncryptService apiEncryptService; private ApiEncryptService apiEncryptService;
@ -45,19 +44,18 @@ public class EncryptResponseAdvice implements ResponseBodyAdvice<ResponseDTO> {
} }
@Override @Override
public ResponseDTO beforeBodyWrite(ResponseDTO body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { public ResponseDTO<Object> beforeBodyWrite(ResponseDTO<Object> body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body.getData() == null) { if (body == null || body.getData() == null) {
return body; return body;
} }
String encrypt = null;
try { 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) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
body.setData(encrypt);
body.setDataType(DataTypeEnum.ENCRYPT.getValue());
return body; return body;
} }
} }