場景:
- 前后端交互中足陨,前端傳給后端的是一個String類型的加密串嫂粟,后端需要解析成Json報文,并反序列為對象墨缘。如何在不侵入業(yè)務(wù)代碼的前提上來實現(xiàn)這個功能星虹?
- 例如需要解析報文中的某些字段,放入到ThreadLocal中镊讼,后續(xù)進行使用宽涌;
- 填充一些公共字段,例如前后端交互過程中狠毯,uid不會信任前端報文傳遞的护糖,而是信任cookie中解析出來的褥芒,需要set到對象中嚼松。
- 對項目報文某些字段進行加密;
- 填充項目報文公共字段锰扶;
這些都可以交由RequestBodyAdvice和ResponseBodyAdvice來實現(xiàn):
@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) {
//當(dāng)存在這個注解時献酗,則進行解析
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轉(zhuǎn)成對象
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;
}
}
}