parent
8fa20f37f1
commit
12e0025ef6
@ -0,0 +1,76 @@ |
|||||||
|
package com.unionmed.framework.spring.mvc.response; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import com.unionmed.framework.crypto.AES; |
||||||
|
import com.unionmed.framework.http.HttpHeaders; |
||||||
|
import com.unionmed.framework.util.ObjectUtils; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.apache.commons.io.IOUtils; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
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.context.request.RequestContextHolder; |
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes; |
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.lang.reflect.Type; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author ianChen |
||||||
|
* @date 2023/6/26 14:02 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
@ControllerAdvice |
||||||
|
public class DecryptRequestBodyAdvice implements RequestBodyAdvice { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private RequestResponseBodyCryptProperties requestResponseBodyCryptProperties; |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException { |
||||||
|
String body = IOUtils.toString(inputMessage.getBody(), HttpHeaders.CHARSET_UTF8); |
||||||
|
if (ObjectUtils.notEmpty(body) && requestResponseBodyCryptProperties.isEnabled() && ObjectUtils.equalsIgnore(inputMessage.getHeaders().getFirst(HttpHeaders.X_DATA_CRYPT_E), HttpHeaders.X_DATA_CRYPT_E_VALUE_TRUE)) { |
||||||
|
body = AES.decrypt(requestResponseBodyCryptProperties.getSk(), body.substring(0, 16), body.substring(16)); |
||||||
|
} |
||||||
|
|
||||||
|
if (log.isDebugEnabled()) { |
||||||
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); |
||||||
|
log.debug("URI: {}, Method: {}", servletRequestAttributes.getRequest().getRequestURI(), servletRequestAttributes.getRequest().getMethod()); |
||||||
|
log.debug("Headers: {}", JSON.toJSONString(inputMessage.getHeaders().toSingleValueMap())); |
||||||
|
log.debug("RequestParameter: {}", servletRequestAttributes.getRequest().getParameterMap() == null ? "" : JSON.toJSONString(servletRequestAttributes.getRequest().getParameterMap())); |
||||||
|
log.debug("RequestBody: {}", body); |
||||||
|
} |
||||||
|
|
||||||
|
InputStream is = IOUtils.toInputStream(body, HttpHeaders.CHARSET_UTF8); |
||||||
|
return new HttpInputMessage() { |
||||||
|
@Override |
||||||
|
public InputStream getBody() throws IOException { |
||||||
|
return is; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public org.springframework.http.HttpHeaders getHeaders() { |
||||||
|
return inputMessage.getHeaders(); |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
||||||
|
return body; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { |
||||||
|
return body; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
package com.unionmed.framework.spring.mvc.response; |
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author ianChen |
||||||
|
* @date 2023/6/26 14:06 |
||||||
|
*/ |
||||||
|
@ConfigurationProperties(prefix = "unionmed.web-mvc.body.crypt.aes") |
||||||
|
public class RequestResponseBodyCryptProperties { |
||||||
|
|
||||||
|
private boolean enabled = false; |
||||||
|
private String sk; |
||||||
|
|
||||||
|
public boolean isEnabled() { |
||||||
|
return enabled; |
||||||
|
} |
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) { |
||||||
|
this.enabled = enabled; |
||||||
|
} |
||||||
|
|
||||||
|
public String getSk() { |
||||||
|
return sk; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSk(String sk) { |
||||||
|
this.sk = sk; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue