赞
踩
package springcloud.dataencode;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
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 springcloud.pojo.ABC;
import springcloud.util.Base64Utils;
import java.security.PrivateKey;
import java.security.PublicKey;
@Component
@ControllerAdvice(basePackages = "springcloud.controller")
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);
//加密
System.out.println("准备加密的数据为"+body);
String encode = Base64Utils.encode(JSON.toJSONString(body));
return encode;
}
public static void main(String[] args) throws Exception {
ABC abc1 = new ABC();
abc1.setName("张三");
abc1.setAge(1);
abc1.setAddress("河南");
// String encode = encode(JSON.toJSONString(abc1));
// System.out.println(encode);
// String decode = decode(encode);
// ABC abc = JSON.parseObject(decode, ABC.class);
// System.out.println(abc);
RSA rsa = new RSA();
//获得私钥
PrivateKey privateKey = rsa.getPrivateKey();
//获得公钥
PublicKey publicKey = rsa.getPublicKey();
//公钥加密,私钥解密
String s1 = JSON.toJSONString(abc1);
byte[] encrypt = rsa.encrypt(s1, KeyType.PublicKey);
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
String s = new String(decrypt);
ABC abc = JSON.parseObject(s, ABC.class);
System.out.println("abc = " + abc);
}
}
package aaa;
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 springcloud.pojo.ABC;
import springcloud.util.Base64Utils;
@Component
@ControllerAdvice(basePackages = "springcloud.controller")
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*******************/
System.out.println("请求的数据为:" + string);
string=string.substring(string.indexOf(":")+2,string.indexOf("=")+1);
String decode = Base64Utils.decode(string);
System.out.println("【解密后的请求数据】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;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。