Springboot框架,实现请求数据解密,响应数据加密的功能。⼀、简要说明:
在做这个功能的时候,参考了很多⽂章,也试了⽤过滤器解决。但总体来说还是很⿇烦,所以换了另⼀种解决⽅案。直接实现RequestBodyAdvice和ResponseBodyAdvice两个接⼝,进⾏加密解密处理。
spring framework 关于RequestBodyAdvice和ResponseBodyAdvice接⼝的作⽤,可参考该⽂档:
(1)blog.csdn/qq_16504067/article/details/73225005
(2) blog.csdn/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 flect.Type;
import MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.verter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.hod.annotation.RequestBodyAdvice;
slf4j.Slf4j;
@Component
@ControllerAdvice(basePackages = "ller")
@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 = Body();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = ad(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 = String();
/*****************进⾏解密start*******************/
log.info("【接受的请求数据】string={}",string);
String decode =Base64Utils.decode(string);
log.info("【解密后的请求数据】decode={}",decode);
/
/把数据放到我们封装的对象中
return new Headers(), new Bytes("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 MethodParameter;
import org.springframework.http.MediaType;
import org.verter.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.hod.annotation.ResponseBodyAdvice;
import com.alibaba.fastjson.JSON;
slf4j.Slf4j;
@Component
@ControllerAdvice(basePackages = "ller")
@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 = JSONString(body));
return encode;
}
}
四、⼯具类
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Utils {
/
**
* 使⽤Base64加密字符串
* @return加密之后的字符串
*
*/
public static String encode(String data){
return Encoder().Bytes(StandardCharsets.UTF_8));
}
/**
* 使⽤Base64解密
* @return解密之后的字符串
*
*/
public static String decode(String data){
return new Decoder().place("\"", "")),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;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论