1.和前端协商好加密算法,小编这里用的是AES代码如下
public class AesEncryptUtil {
//使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
private static final String KEY = ConstantFilter.S_KEY;
private static final String IV = ConstantFilter.IV_PARAMETER;
public static String encrypt(String data, String key, String iv) throws Exception {
try {
Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”);//"算法/模式/补码方式"NoPadding PkcsPadding
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if(plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), “AES”);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new Base64().encodeToString(encrypted);
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public static String desEncrypt(String data, String key, String iv) throws Exception {
try {
byte[] encrypted1 = new Base64().decode(data);
Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), “AES”);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public static String encrypt(String data) throws Exception {
return encrypt(data, KEY, IV);
}
public static String desEncrypt(String data) throws Exception {
return desEncrypt(data, KEY, IV);
}
}
2.对所有返回数据进行加密,具体逻辑是这样的:
(一)获取前端传入的请求头,根据请求头内的值日判断接口是否加密
(二)对数据加密,并且释放掉内存否则会内存溢出
(三)正常返回数据
(四)直接贴代码
public class ResFilter implements GlobalFilter, Ordered {
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpResponse originalResponse = exchange.getResponse();
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
//获取前端传入的请求头,如果授权即默认为加密处理
ServerHttpRequest request = exchange.getRequest();
String token = request.getHeaders().getFirst(ConstantFilter.BG_DEBUG_KEY);
if(ConstantFilter.REQ_RES_ENCRYPT.equals(token)) {
log.info(“返回数据加密状态为【加密】,当前时间:” + new Date());
long lStart = System.currentTimeMillis();
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono writeWith(Publisher extends DataBuffer> body) {
if(body instanceof Flux) {
Flux extends DataBuffer> fluxBody = (Flux extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
// 释放掉内存
DataBufferUtils.release(join);
// 返回值得字符串
String str = new String(content, Charset.forName("UTF-8"));
log.info("返回的源数据》》小程序:" + str);
log.info("返回的源数据》》小程序:" + str);
// 把下面加密方法改成你自己的
// String encryptStr = Base64.getEncoder().encodeToString(Encodes.encryptAES1(str));
String encryptStr = null;
try {
encryptStr = AesEncryptUtil.encrypt(str);
}
catch(Exception e) {
e.printStackTrace();
}
log.info("返回的加密数据秘钥为:" + "[" + encryptStr + "]");
log.info("返回的加密数据秘钥为:" + "[" + encryptStr + "]");
originalResponse.getHeaders().setContentLength(encryptStr.getBytes().length);
return bufferFactory.wrap(encryptStr.getBytes());
}));
}
return super.writeWith(body);
}
};
// 将响应替换为装饰器
long lUseTime = System.currentTimeMillis() - lStart;
log.info("返回加密逻辑耗时为:" + lUseTime + ",毫秒");
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
// else if(ConstantFilter.REQ_RES_NALMORE.equals(token)) {
else {
long lStart = System.currentTimeMillis();
log.info("返回数据加密状态为【不加密】,当前时间:" + new Date());
ServerHttpResponseDecorator decoratedResponse1 = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono writeWith(Publisher extends DataBuffer> body) {
if(body instanceof Flux) {
Flux extends DataBuffer> fluxBody = (Flux extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
DataBuffer join = dataBufferFactory.join(dataBuffers);
byte[] content = new byte[join.readableByteCount()];
join.read(content);
// 释放掉内存
DataBufferUtils.release(join);
// 返回值得字符串
String str = new String(content, Charset.forName("UTF-8"));
log.info(str);
// 把下面加密方法改成你自己的
String encryptStr = str;
originalResponse.getHeaders().setContentLength(encryptStr.getBytes().length);
return bufferFactory.wrap(encryptStr.getBytes());
}));
}
return super.writeWith(body);
}
};
//将响应替换为装饰器
long lUseTime = System.currentTimeMillis() - lStart;
log.info("返回参数不加密逻辑耗时为:" + lUseTime + ",毫秒");
return chain.filter(exchange.mutate().response(decoratedResponse1).build());
}
// return null;
}
@Override
public int getOrder() {
return -1;
}
3.对请求头进行拦截,具体逻辑如下:



