KKSU
2024-07-25 adee5d271a70cbcb9ab45cec00795c9a7b34f6bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cc.mrbird.febs.common.advise;
 
import cc.mrbird.febs.common.utils.AppContants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
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.RequestBodyAdvice;
 
import java.io.IOException;
import java.lang.reflect.Type;
 
/**
 * @author wzy
 * @date 2022-06-14
 **/
@ControllerAdvice
public class MyRequestBodyAdvise implements RequestBodyAdvice {
 
    @Value("${system.debug}")
    private boolean isDebug;
 
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
 
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
 
        if(!AppContants.ENCRYPT_METHOD.contains(methodParameter.getMethod().getName()) || isDebug) {
            return inputMessage;
        }
 
        return new MyHttpInputMessage(inputMessage.getBody());
    }
 
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return body;
    }
 
    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return o;
    }
 
}