一、简要说明:
在做这个功能的时候,参考了很多文章,也试了用过滤器解决。但总体来说还是很麻烦,所以换了另一种解决方案。直接实现RequestBodyAdvice和ResponseBodyAdvice两个接口 ,进行加密解密处理。
关于RequestBodyAdvice和ResponseBodyAdvice接口的作用,可参考该文档:
(1)https://blog.csdn.net/qq\_16504067/article/details/73225005
(2) https://blog.csdn.net/jing956899449/article/details/54315048
二、解密 DecodeRequestBodyAdvice
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import lombok.extern.slf4j.Slf4j;
@Component
@ControllerAdvice(basePackages = "com.kiki.controller")
@Slf4j
public class DecodeRequestBodyAdvice implements RequestBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
//这里设置成false 它就不会再走这个类了
return true;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage request, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
//这个request其实就是入参 可以从这里获取流
//入参放在HttpInputMessage里面 这个方法的返回值也是HttpInputMessage
InputStream inputStream = request.getBody();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
//获取请求数据
String string = stringBuilder.toString();
/*****************进行解密start*******************/
log.info("【接受的请求数据】string={}",string);
String decode =Base64Utils.decode(string);
log.info("【解密后的请求数据】decode={}",decode);
//把数据放到我们封装的对象中
return new MyHttpInputMessage(request.getHeaders(), new ByteArrayInputStream(decode.getBytes("UTF-8")));
}
@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;
}
//这里实现了HttpInputMessage 封装一个自己的HttpInputMessage
static class MyHttpInputMessage implements HttpInputMessage {
HttpHeaders headers;
InputStream body;
public MyHttpInputMessage(HttpHeaders headers, InputStream body) {
this.headers = headers;
this.body = body;
}
@Override
public InputStream getBody() throws IOException {
return body;
}
@Override
public HttpHeaders getHeaders() {
return headers;
}
}
}
三、加密 EncodeResponseBodyAdvice
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
@Component
@ControllerAdvice(basePackages = "com.kiki.controller")
@Slf4j
public class EncodeResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
System.out.println("response-------->"+body);
//加密
log.info("【接受到的数据】encode={}",body);
String encode = Base64Utils.encode(JSON.toJSONString(body));
return encode;
}
}
四、工具类
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Utils {
/**
* 使用Base64加密字符串
* @return 加密之后的字符串
*
*/
public static String encode(String data){
return Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
}
/**
* 使用Base64解密
* @return 解密之后的字符串
*
*/
public static String decode(String data){
return new String(Base64.getDecoder().decode(data.replace("\"", "")),StandardCharsets.UTF_8);
}
public static void main(String[] args) {
String encode = encode("{\"key\":\"kiki123\",\"value\":\"hello lmbx\"}");
System.out.println(encode);
String decode = decode("eyJrZXkiOiIxMjMiLCJ2YWx1ZSI6ImFhYSJ9");
System.out.println(decode);
}
}
五、bean
import lombok.Data;
@Data
public class DataBean {
private String key;
private String value;
}
六、Controller
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kiki.bean.DataBean;
@RestController
@RequestMapping("/api/")
public class DataController {
@PostMapping("test")
public DataBean test(@RequestBody DataBean dataBean) {
System.out.println("controller::::"+dataBean);
dataBean.setKey("456");
dataBean.setValue("0000");
return dataBean;
}
}