版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013780371/article/details/86293613
RequestBodyAdvice可以理解为在@RequestBody之前需要进行的 操作,ResponseBodyAdvice可以理解为在@ResponseBody之后进行的操作,所以当接口需要加解密时,在使用@RequestBody接收前台参数之前可以先在RequestBodyAdvice的实现类中进行参数的解密,当操作结束需要返回数据时,可以在@ResponseBody之后进入ResponseBodyAdvice的实现类中进行参数的加密。
先来看一下源码RequestBodyAdvice
public interface RequestBodyAdvice {
/**是否支持使用RequestBodyAdvice
*/
boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType);
/**
*/
Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
/**
在数据读出
之前可以做的事情 */
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;
/**
*/
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
}
再来看一下源码RequestBodyAdvice
public interface ResponseBodyAdvice<T> {
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
/*主要有两个方法 boolean supports判断是否支持, beforeBodyWrite在写入参数列表之前要做的操作
*/
T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
完成加密解密的操作
首先自定义一个注解类(用来后期判断是否使用加解密)
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Encrypt {
}
加密时需要新建一个ParamEncryptResponseBodyAdvice类继承于ResponseBodyAdvice
@ControllerAdvice
public class ParamEncryptResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return returnType.hasMethodAnnotation(Encrypt.class);//判断是否支持加密,必须有@Encrypt注解才支持
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType,
org.springframework.http.MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
System.out.println("此处进行数据加密");
return body;
}
}
解密需要新建一个类继承于RequestBodyAdvice
@ControllerAdvice
public class ParamDecryptRequestBodyAdvice implements RequestBodyAdvice {
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return methodParameter.hasParameterAnnotation(Encrypt.class);//判断解密。必须使用@Encrypt注解才进行解密
}
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return o;
}
public HttpInputMessage beforeBodyRead( HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
return new HttpInputMessage() {
public InputStream getBody() throws IOException {
System.out.println("此处进行解密");
return new ByteArrayInputStream();
}
public HttpHeaders getHeaders() {
return httpInputMessage.getHeaders();
}
};
}
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return o;
}
}
在controller层调用时
1.不加密不解密
@PostMapping("/search")
@ResponseBody
public ResultForSearch searchTicket(@RequestBody SearchReq_Param search_Param){
return null;
}
2.加密解密
@PostMapping("/search")
@ResponseBody
@Encrypt
public ResultForSearch searchTicket(@Encrypt@RequestBody SearchReq_Param search_Param){
return null;
}
3.只加密不解密
@PostMapping("/search")
@ResponseBody
@Encrypt
public ResultForSearch searchTicket(@RequestBody SearchReq_Param search_Param){
return null;
}
————————————————
版权声明:本文为CSDN博主「青橙工作室-吴」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013780371/article/details/86293613