com.jl.encrypt.JRequestBodyAdvice Maven / Gradle / Ivy
The newest version!
package com.jl.encrypt;
import com.jl.encrypt.annotate.JDecode;
import com.jl.encrypt.config.SecretKeyConfig;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
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.lang.reflect.Type;
/**
* body入参拦截
*/
@ControllerAdvice
@AllArgsConstructor
public class JRequestBodyAdvice implements RequestBodyAdvice {
private SecretKeyConfig secretKeyConfig;
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
return methodParameter.getMethod().isAnnotationPresent(JDecode.class) && secretKeyConfig.isOpen() ? true : false;
}
@Override
@SneakyThrows
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class extends HttpMessageConverter>> converterType) {
if (parameter.getMethod().isAnnotationPresent(JDecode.class)) {
return new JHttpInputMessage(inputMessage, secretKeyConfig.getAes(), secretKeyConfig.getRsa(), secretKeyConfig.isShowLog());
}
return inputMessage;
}
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
return o;
}
@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
return o;
}
}