Inherit the RequestBodyAdvice and ResponseBodyAdvice return json interface encryption and decryption

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: HTTPS: //blog.csdn.net/u013780371/article/details/86293613
RequestBodyAdvice be appreciated that to operate before the required @RequestBody, ResponseBodyAdvice can be understood as an operation performed after @ResponseBody, so when the interface needs to add decryption, decryption parameters may be performed first in RequestBodyAdvice implementation class prior to use @RequestBody receiving reception parameters required when the operation ends to return data, may enter the encryption parameter ResponseBodyAdvice implementation class after @ResponseBody.

First look at the source code 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);

    /**

In the data read 
  before you can do something * /
    HttpInputMessage beforeBodyRead (HttpInputMessage inputMessage, MethodParameter the Parameter,
            ?? Type targetType, Class <the extends HttpMessageConverter <>> converterType) throws IOException;

    /**
    
     */
    Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
            Type targetType, Class<? extends HttpMessageConverter<?>> converterType);

}
 

 

 

 

Then look at the source code RequestBodyAdvice

public interface ResponseBodyAdvice<T> {
    boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);

/ * There are two ways to judge whether to support boolean supports, beforeBodyWrite before writing the parameter list to do the operation

*/
    T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request, ServerHttpResponse response);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

To complete the encryption and decryption operations

First, a custom annotation class (late for determining whether to use the encryption and decryption)

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented

public @interface Encrypt {
     
    
}

ParamEncryptResponseBodyAdvice need to create a class that inherits encryption to ResponseBodyAdvice

@ControllerAdvice
public class ParamEncryptResponseBodyAdvice implements ResponseBodyAdvice {

@Override
    public Boolean the Supports (MethodParameter the returnType, Class converterType) {
        return returnType.hasMethodAnnotation (Encrypt.class); // determines whether or not support encryption, must be supported only annotations @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;
    }

    
}

Decryption need to create a class inherits from RequestBodyAdvice

@ControllerAdvice
public class ParamDecryptRequestBodyAdvice implements RequestBodyAdvice {


    the Supports Boolean public (?? MethodParameter MethodParameter, the Type type, Class <the extends HttpMessageConverter types <>> aClass) {
        return methodParameter.hasParameterAnnotation (Encrypt.class); // judgment decryption. Must only be decrypted using @Encrypt comment
    }


    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;
    }
}

When the controller layer calls

1. does not decrypt the encrypted

@PostMapping("/search")
@ResponseBody
    public  ResultForSearch searchTicket(@RequestBody  SearchReq_Param search_Param){

return null;

}

2. encryption and decryption

@PostMapping("/search")
@ResponseBody   

@Encrypt
    public  ResultForSearch searchTicket(@Encrypt@RequestBody  SearchReq_Param search_Param){

return null;

}

3. Only encryption does not decrypt

@PostMapping("/search")
@ResponseBody   

@Encrypt
    public  ResultForSearch searchTicket(@RequestBody  SearchReq_Param search_Param){

return null;

}


----------------
Disclaimer: This article is CSDN blogger "Green Orange studio - Wu 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/u013780371/article/details/86293613

Published 51 original articles · won praise 80 · views 930 000 +

Guess you like

Origin blog.csdn.net/xiyang_1990/article/details/103092732