场景:
- 前后端交互中,前端传给后端的是一个String类型的加密串,后端需要解析成Json报文,并反序列为对象。如何在不侵入业务代码的前提上来实现这个功能?
- 例如需要解析报文中的某些字段,放入到ThreadLocal中,后续进行使用;
- 填充一些公共字段,例如前后端交互过程中,uid不会信任前端报文传递的,而是信任cookie中解析出来的,需要set到对象中。
- 对项目报文某些字段进行加密;
- 填充项目报文公共字段;
这些都可以交由RequestBodyAdvice和ResponseBodyAdvice来实现:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DecryptRequestBody {
}
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.commons.io.IOUtils;
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 com.alibaba.fastjson.JSON;
@ControllerAdvice
public class DecryptRequestBodyAdvice extends RequestBodyAdviceAdapter {
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
//当存在这个注解时,则进行解析
return methodParameter.hasParameterAnnotation(DecryptRequestBody.class);
}
//报文解析前进行出来
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
DecryptHttpInputMessage message = new DecryptHttpInputMessage(inputMessage);
//todo message可以做各种处理,典型的是将加密串解析成Json,以适配RequestBody转成对象
return message;
}
//报文解析后,可以做的工作
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
//todo 例如强制为对象,来赋予公共值;例如读取出特定的值
String jsonString = JSON.toJSONString(body);
Map<String, Object> params = JSON.parseObject(jsonString);
Object language = params.get("language");
//...
return body;
}
/**
* 自定义一个HttpInputMessage用于存储新的信息
*/
public static class DecryptHttpInputMessage implements HttpInputMessage {
private final Charset charset = UTF_8;
private HttpHeaders headers;
private InputStream body;
private String bodyData;
public DecryptHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
headers = inputMessage.getHeaders();
bodyData = IOUtils.toString(inputMessage.getBody(), charset);
body = IOUtils.toInputStream(bodyData, charset);
}
@Override
public InputStream getBody() throws IOException {
return body;
}
@Override
public HttpHeaders getHeaders() {
return headers;
}
public String getBodyData() {
return bodyData;
}
public void setBody(InputStream inputStream) {
body = inputStream;
}
/**
* 写入新的body流
*/
public void setBody(String newBodyData) {
body = IOUtils.toInputStream(newBodyData, charset);
}
public void setBodyData(String bodyDataStr) {
bodyData = bodyDataStr;
}
}
}
网友评论