perf: 加解密数据处理
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.tiesheng.encrypt.config;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.util.config.EncryptConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
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.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
|
||||
|
||||
|
||||
@Autowired
|
||||
EncryptConfig encryptConfig;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||
Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
|
||||
try {
|
||||
return new DecryptHttpInputMessage(inputMessage, encryptConfig);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
||||
static class DecryptHttpInputMessage implements HttpInputMessage {
|
||||
|
||||
private HttpHeaders headers;
|
||||
private InputStream body;
|
||||
|
||||
DecryptHttpInputMessage(HttpInputMessage inputMessage, EncryptConfig encryptConfig) throws Exception {
|
||||
this.headers = inputMessage.getHeaders();
|
||||
String bodyStr = IoUtil.read(inputMessage.getBody(), CharsetUtil.CHARSET_UTF_8);
|
||||
String encryptData = JSONUtil.parseObj(bodyStr).getStr("encryptData");
|
||||
if (!StrUtil.isEmpty(encryptData)) {
|
||||
String decrypt = encryptConfig.decrypt(encryptData);
|
||||
this.body = IoUtil.toStream(decrypt, Charset.defaultCharset());
|
||||
} else {
|
||||
this.body = IoUtil.toStream(bodyStr, Charset.defaultCharset());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user