过程备注:
1.网关多个 filter 重复调用的问题
比如:负载均衡,跨域设置等过滤器。可在重复调用的重写 filter 中增加判断:
if(ServerWebExchangeUtils.isAlreadyRouted(exchange)){
return chain.filter(exchange);
}
2.微服务生成的密钥对以json格式保存数据库和Redis,校验token有效性时,优先读取Redis。返回网关的响应报文,直接返回SM2Key对象(从数据库或Redis取出缓存做个反序列化)。网关返回给请求方只是客户端公私钥,而网关的本地缓存则保存整个SM2Key对象(这个对象不需要序列化为响应报文内容),供后续请求加解密处理。
3.刷新token也需要返回新的密钥对。
4.解密过滤器中的GetURI重写方法,会重复调用。因为其他过滤器如果有调用到GetURI,会调用解密过滤器的GetURI,导致重复解密,暂时以“单例”模式的笨办法处理。
1、引用工具包
引用 Hutool 和加密算法库 Bouncy Castle Crypto
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.72</version>
</dependency>
2、创建sm2密钥对对象
package com.akim.cloud.framework.common.util.crypto;
import lombok.Data;
/**
* @author akim
* @version 1.0
* @date 2023/11/2 14:10
* @desc sm2密钥对
*/
@Data
public class SM2Key {
/**
* 服务端加密公钥,对应私钥由客户端持有(clientPrivateKey)
*/
private String serverPublicKey;
/**
* 服务端解密私钥,对应公钥由客户端持有(clientPublicKey)
*/
private String serverPrivateKey;
/**
* 客户端加密公钥,对应私钥由服务端持有(serverPrivateKey)
*/
private String clientPublicKey;
/**
* 客户端解密私钥,对应公钥由服务端持有(serverPublicKey)
*/
private String clientPrivateKey;
}
3、创建sm2工具类
package com.akim.cloud.framework.common.util.crypto;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
/**
* @author akim
* @version 1.0
* @date 2023/11/2 14:10
* @desc sm2工具类
*/
public class SM2Util {
/**
* 生成前后端加解密密钥对
*
* @return
*/
public static SM2Key generate() {
SM2Key sm2Key = new SM2Key();
SM2 sm2 = SmUtil.sm2();
sm2Key.setServerPublicKey(HexUtil.encodeHexStr(((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false)));
sm2Key.setClientPrivateKey(HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey())));
sm2 = SmUtil.sm2();
sm2Key.setClientPublicKey(HexUtil.encodeHexStr(((BCECPublicKey) sm2.getPublicKey()).getQ().getEncoded(false)));
sm2Key.setServerPrivateKey(HexUtil.encodeHexStr(BCUtil.encodeECPrivateKey(sm2.getPrivateKey())));
return sm2Key;
}
public static String encrypt(String publicKey, String data) {
return SmUtil.sm2(null, publicKey)
.encryptHex(data.getBytes(), KeyType.PublicKey)
.substring(2); // 去掉04
}
public static String decrypt(String privateKey, String data) {
return SmUtil.sm2(privateKey, null)
.decryptStr(data.startsWith("04") ? data : "04" + data, KeyType.PrivateKey);
}
// public static void main(String[] args) {
// try {
// SM2Key sm2Key = SM2Util.generate();
// String testStr = "我是测试内容";
// // 服务端加密
// String enStr = SM2Util.encrypt(sm2Key.getServerPublicKey(), testStr);
// System.out.println("服务端加密结果:" + enStr);
// // 客户端解密
// String deStr = SM2Util.decrypt(sm2Key.getClientPrivateKey(), enStr);
// System.out.println("客户端解密结果:" + deStr);
//
// // 客户端加密
// String enStr2 = SM2Util.encrypt(sm2Key.getClientPublicKey(), testStr);
// System.out.println("客户端加密结果:" + enStr);
// // 服务端解密
// String deStr2 = SM2Util.decrypt(sm2Key.getServerPrivateKey(), enStr2);
// System.out.println("服务端解密结果:" + deStr2);
// } catch (Exception e) {
// System.out.println(e.getMessage());
// }
// }
}
4、创建加密配置类
package com.akim.cloud.gateway.common.crypto.config;
import com.akim.cloud.gateway.common.crypto.adapter.CryptoFormatterAdapter;
import com.akim.cloud.gateway.filter.crypto.DecryptFilter;
import com.akim.cloud.gateway.filter.crypto.EncryptFilter;
import com.akim.cloud.gateway.common.crypto.rewrite.RequestRewriter;
import com.akim.cloud.gateway.common.crypto.rewrite.ResponseRewriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
* @author akim
* @version 1.0
* @date 2023/11/5 23:21
* @desc 加密配置
* 后续可以通过配置文件增加加解密类型进行拓展
*/
@Configuration
@ConditionalOnProperty(value = "akim.secret.enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class CryptoConfiguration {
/**
* 免加密接口配置
*/
public static final String EXCLUDE_PATH_CONFIG_KEY =