一、问题
之前写过一篇《springboot修改接口入参出参实现入参加密出参解密》,代码一直跑着都没问题,但是最近发现入参特别长的时候,inputMessage.getBody() 这段代码拿不到完整的入参,会被截断。
看了下源码,好像默认它转换的时候就截断了。
二、解决方法
看了下源码,RequestBodyAdviceAdapter 这个静态类除了 beforeBodyRead 这个方法外,还有个 afterBodyRead 方法,原来我一直以为 beforeBodyRead 是controller 层之前执行,afterBodyRead 是在controller层之后执行,后来我发现我理解错了,它们都是在controller层之前执行。
并且,beforeBodyRead 方法是在 afterBodyRead 方法之前执行,走这两个方法之前都会走一次 supports 方法来判断是否执行。
于是我就把解密逻辑挪到了 afterBodyRead 方法里,就不存在入参超长被截断的问题了。
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.utils.JSONUtils;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
/**
* @Author: 夏威夷8080
* @Date: 2022/7/8 20:28
*/
@ControllerAdvice
@Slf4j
public class DecryptRequestAdvice extends RequestBodyAdviceAdapter {
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return methodParameter.hasMethodAnnotation(Decrypt.class) || methodParameter.hasParameterAnnotation(Decrypt.class);
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
try {
String requestBody = JSONUtils.serializeObject(body);
if (JSONObject.isValid(requestBody)) {
JSONObject jsonObject = JSONObject.parseObject(requestBody);
// 结合自己业务针对获取到的requestBody内容进行修改解密等操作
String decryptData = aesUtil.decrypt(requestBody , "www.cnblogs.com/shamo89");
log.info("接口解密后的入参:{}", decryptData);
body = JSONObject.parseObject(decryptData, targetType);
} else {
throw new IllegalArgumentException("获取到的入参不是合法的json格式!");
}
} catch (Exception e) {
log.error("接口入参解密出错:{}", Throwables.getStackTraceAsString(e));
}
return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
}
三、拓展
其实RequestBodyAdviceAdapter本质也是切面,所以也可以借助切面来完成。
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Objects;
/**
* @Author: 夏威夷8080
* @Date: 2022/7/13 13:42
*/
@Aspect
@Component
@Slf4j
public class DecryptInterceptor {
@Pointcut("@annotation(com.ls.nat.census.server.annotation.Decrypt)")
public void decrypt() {
}
/**
* 在方法执行之前、后执行该方法
* 设置需要执行该方法的类路径和注解
*/
@Around("decrypt()")
public Object interceptor(ProceedingJoinPoint point) throws Throwable {
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
// 获取请求参数
Object[] args = point.getArgs();
if (args == null || args.length <= 0) {
throw new CustomException("此接口缺少入参,无需解密!");
}
Object arg = args[0];
String requestBody = JSONUtil.toJsonStr(arg);
log.info("------requestBody-入参:{}", requestBody);
// 根据自己业务需求决定这边如何解密
String decryptData = aesUtil.decryptByHex(requestBody);
log.info("接口解密后的入参:{}", decryptData);
// 获取接收参数的类全路径
String classPath = args[0].getClass().getName();
// 把处理后的参数放回去
args[0] = JSONUtil.toBean(decryptData, Class.forName(classPath));
// 获取响应数据
Object result = point.proceed(args);
return result;
}
}
beforeBodyRead