文章目录
- 前言
- [Spring Cloud 第一代](#Spring Cloud 第一代)
-
- [Spring Cloud Alibaba](#Spring Cloud Alibaba)
- 低版本架构不支持,取巧实现
-
前言
之前就想着做一个汇总的记录,在实际项目开发中,公司、客户等群体对数据安全性问题,都是很看重的,结合实际的开发,本次做一个各项分布式微服务架构的加密配置说明汇总。
Spring Cloud 第一代
在第一代 Spring Cloud
架构体系中,一般是将公用的,或者配置随环境需要变动的,采用Config
配置中心进行集中管理。Spring Cloud Config
配置中心本身就具备配置文件的加解密配置处理
。
注意标识{cipher}
具体实现方式如下所示:
config server的加解密功能依赖Java Cryptography Extension(JCE)
本次开发测试使用的是 jdk 1.8,所以jdk 1.8 的jce 下载地址为:
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
下载并解压,将其中的jar包覆盖到JDK/jre/lib/security目录中。
关于 jce_policy-8.zip 我这里已经下载好了,具体需要用到的可以去我的github中查找!
《jdk1.8 jce依赖文件》
《jce_policy安装【java密码扩展无限制权限策略文件安装】》
注意:
安装jdk,需要在jre/lib/security中粘贴上述中的两个jar文件;
同时,如果安装了jre,也需要在jre中添加jce的jar文件。


1、创建config server项目并加入加解密key
由于此时的加解密key不能被修改和覆盖,所以需要创建bootstrap.yml
文件进行配置。
bootstrap.yml
yml
复制代码
### 添加加解密key
## (不能被覆盖和修改,所以必须配置在bootstrap.yml中)
encrypt:
key: xiangjiaobunana
配置好信息后,其他配置沿用之前的demo配置。
application.yml:
yml
复制代码
###服务名称(服务注册到eureka名称)
spring:
application:
name: springcloud-config-service
## Config Server 配置中心Service配置信息
## 配置文件所在的 git 仓库地址
spring.cloud.config.server.git.uri: https://github.com/765199214/springcloud2.0-config-service.git
## 配置文件再哪个文件夹下
spring.cloud.config.server.git.search-paths: respo
## 配置 clone-on-start 启动时就clone仓库到本地,默认是在配置被首次请求时,config server 才会 clone git 仓库
spring.cloud.config.server.git.clone-on-start: true
## 配置默认 git clone 至指定的磁盘或文件夹内(linux 只有一个 / 根;windows 会进入项目所在的磁盘下)
spring.cloud.config.server.git.basedir: /data/config server/
2、启动项目,进行数据加密
Config Server 本身就为此提供了加解密操作的接口,环境配置中只需要请求 /encrypt与/decrypt 即可实现。
- 加密:
成功启动项目后,请求下列接口实现加密的输出:
curl http://localhost:3000/encrypt -d '要加密的信息'
post 请求

加密后:

- 解密:


初步的加密解密实现了,此时又该如何应用到实际的项目中去呢?
3、实际项目中的测试server
修改 github 中的文件信息。
yml
复制代码
springboot:
datasources:
username: root
password: '{cipher}38785234edc0396a0cc887cb8c737546f1fe244c5baaeae253789744d9a8484b'
## {cipher}只是一个标记,方便springcloud去识别判断,如果没加标记,则不会进行解密操作
修改config server 中的文件夹扫描路径:
spring.cloud.config.server.git.search-paths: respo,en*
重启项目,请求测试:
http://localhost:3000/config-encrypt-dev.yml

查看本地缓存的git 信息得知:

总结:
本地的文件依旧是 密文!
他只会在内存中进行解密操作!
问:\]如果不想在 server 中就进行解密,而是想在client中再解密(server拿到依旧是密文,client解密),我又该如何操作?
> 增加下列配置信息:
>
> spring.cloud.config.server.encrypt.enabled: false
## Spring Cloud Alibaba
`Spring cloud Alibaba`在国内用的较多,相比第一代的cloud而言,`Spring cloud Alibaba` 的 `Nacos`本身就具备`eureka/Zookeper 注册中心`、`Config 配置中心`、`Bus 配置消息通知`等功能。
并且第一代cloud早就未出现后续的维护更新操作,论安全方面,cloud alibaba更加具备。
> 但`Cloud Alibaba`的`Naocs`,在`> 2.0.4`版本中,才支持`配置文件的加密`。
关于配置和使用,参考官方文档:
[Nacos 配置文件加密操作](https://nacos.io/zh-cn/docs/v2/plugin/config-encryption-plugin.html)
## 低版本架构不支持,取巧实现
如果实际开发中,选择的`架构版本比较低(相对支持版)`或者`非分布式架构`,然道就不能支持配置的加密解密了?
> 本次只对`数据源`的配置进行`配置加密解密`操作,其他配置项,可以使用`自定义加解密`方式实现。
### 无加密配置,联调环境问题
引入pom依赖,完整如下:
```xml
org.springframework.boot
spring-boot-starter
2.1.4.RELEASE
org.springframework.boot
spring-boot-starter-web
2.1.4.RELEASE
org.projectlombok
lombok
1.16.20
io.springfox
springfox-swagger2
2.4.0
io.springfox
springfox-swagger-ui
2.4.0
com.github.xiaoymin
swagger-bootstrap-ui
1.9.6
com.alibaba
fastjson
1.2.83
org.slf4j
slf4j-api
1.7.26
org.slf4j
slf4j-log4j12
1.7.26
com.baomidou
dynamic-datasource-spring-boot-starter
3.6.1
mysql
mysql-connector-java
com.baomidou
mybatis-plus-boot-starter
3.4.0
com.baomidou
mybatis-plus-core
3.4.0
compile
junit
junit
test
org.springframework
spring-test
test
org.springframework.boot
spring-boot-test
test
```
其中核心点:
```xml
com.baomidou
dynamic-datasource-spring-boot-starter
3.6.1
mysql
mysql-connector-java
com.baomidou
mybatis-plus-boot-starter
3.4.0
com.baomidou
mybatis-plus-core
3.4.0
compile
```
数据源配置
```yml
server:
port: 80
spring:
datasource:
dynamic:
datasource:
xiangjiao:
url: jdbc:mysql://xxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
primary: xiangjiao # 主连接
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
```
编写调试查询接口
```java
import cn.xj.model.CommonResult;
import cn.xj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test1")
public class TestController {
@Autowired
UserService userServiceImpl;
@GetMapping("/test1")
public CommonResult test1(Integer id){
return CommonResult.success(userServiceImpl.test1(id));
}
}
```
服务层
```java
import cn.xj.dao.UserMapper;
import cn.xj.model.Users;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public String test1(Integer id){
List users = userMapper.selectList(Wrappers.lambdaQuery(Users.class).eq(Users::getUserId,id));
Users users1 = users.get(0);
return users1.getUserName();
}
}
```
dao层
```java
import cn.xj.model.Users;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserMapper extends BaseMapper {
}
```
请求测试:
> http://localhost/test1/test1?id=2
> 
### 加密数据源配置
和`SpringCloud `第一代中的`{cipher}`标识一样,`dynamic datasource`当然也需要一个标识用来区分加密和不加密的配置内容,默认使用`ENC(xxxxx)`区别。
> 在`3.5.0`版本开始,可以自定义配置,这个等会再说。
`dynamic-datasource`支持对数据源配置的`url`、`username`、`password`进行加密配置。加密的方法可以参考下面的代码。
```java
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
public class Demo {
public static void main(String[] args) throws Exception {
String password = "root";
//使用默认的publicKey ,建议还是使用下面的自定义
String encodePassword = CryptoUtils.encrypt(password);
System.out.println(encodePassword);
}
//自定义publicKey
public static void main(String[] args) throws Exception {
String[] arr = CryptoUtils.genKeyPair(512);
System.out.println("privateKey: " + arr[0]);
System.out.println("publicKey: " + arr[1]);
System.out.println("url: " + CryptoUtils.encrypt(arr[0], "jdbc:mysql://127.0.0.1:3306/order"));
System.out.println("username: " + CryptoUtils.encrypt(arr[0], "root"));
System.out.println("password: " + CryptoUtils.encrypt(arr[0], "123456"));
}
}
```

对上面`无加密`的数据信息进行加密,加密后的yml配置如下所示:
> 注意: 加密后的密文在配置前,需要增加标识。
```yml
server:
port: 80
spring:
datasource:
dynamic:
datasource:
xiangjiao:
url: jdbc:mysql://xxxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: ENC(VZamSTMi224AH6RUtJGXNldiDp/XEL2ozRhBUu/o9ChodT4JEb9kE/j0EFhXKbjsfvLVacUW0AUzetA6OrNJug==)
password: ENC(VZamSTMi224AH6RUtJGXNldiDp/XEL2ozRhBUu/o9ChodT4JEb9kE/j0EFhXKbjsfvLVacUW0AUzetA6OrNJug==)
driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
primary: xiangjiao # 主连接
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
```
再次重启项目,测试

### 原理探究
在`dynamic-datasource`中的`com.baomidou.dynamic.datasource.event.EncDataSourceInitEvent`中,定义了正则表达式,当识别到密文用`ENC(xxx)`包装时,会触发解析明文操作,进行数据配置的还原与包装。

当然这里可以`自定义其他加密`方式,只需要按照官方源码中的例子,定义解析类,然后注入到spring容器中,如下:

这里的`@ConditionalOnMissingBean`注解起了关键性的作用。
> 如果不存在`DataSourceInitEvent`对象的实例bean,才会注入官方默认的`EncDataSourceInitEvent`实例!
### 自定义加密解密器实现数据源加密解密配置
下面以`CCM`加密解密作为一个自定义操作。
导入CCM所需要的jar包。

并加载至当前项目中。
编写`CCM`加密解密的工具类。
```java
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Security;
public class CcmUtils {
static byte[] keyBytes = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16};
static byte[] nonce = {'a','b','c','d','e','f','g','h','i','j','k','l'};
public static void main(String[] args) throws Exception {
byte[] bytes = "root".getBytes();
System.out.println(new String(bytes,"utf-8")); // root
String s = DatatypeConverter.printHexBinary(bytes);
System.out.println(s); // 726F6F74
// 加密
byte[] roots = encrypt("root");// 3914D177814A01A1
String s1 = DatatypeConverter.printHexBinary(roots);
// 解密
String decrypt = decrypt(s1);
System.out.println(decrypt);
}
// 加密
public static byte[] encrypt(String str) throws Exception {
Security.addProvider(new BouncyCastleProvider());
//32 mac 4字节 32位
GCMParameterSpec parameterSpec = new GCMParameterSpec(32, nonce);
Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameterSpec);
System.out.println(DatatypeConverter.printHexBinary(cipher.doFinal(str.getBytes())));
return cipher.doFinal(str.getBytes());
}
// 解密
public static String decrypt(String str) throws Exception {
Security.addProvider(new BouncyCastleProvider());
//32 mac 4字节 32位
GCMParameterSpec parameterSpec = new GCMParameterSpec(32, nonce);
Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding");
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameterSpec);
// String 转 十六进制的 数组
// 3914D177814A01A1 -> 0x39,0x14
byte[] cipStr = HexString2Bytes(str);
System.out.println(DatatypeConverter.printHexBinary(cipher.doFinal(cipStr)));
return new String(cipher.doFinal(cipStr),"utf-8");
}
// 十六进制字符串 转 十六进制 byte 数组
public static byte[] HexString2Bytes(String src) {
byte[] ret = new byte[src.length() / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < tmp.length / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
// 十六进制的byte数组 转 十六进制字符串
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
```
root 加密后的字符串如下:
> 3914D177814A01A1
编写新的配置密文解析器,并定义加密头标识`CCM(xxxx)`。
```java
import cn.xj.util.CcmUtils;
import com.baomidou.dynamic.datasource.event.DataSourceInitEvent;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class CcmDataSourceInitEvent implements DataSourceInitEvent {
/**
* 自定义标识
*/
private static final Pattern ENC_PATTERN = Pattern.compile("^CCM\\((.*)\\)$");
@Override
public void beforeCreate(DataSourceProperty dataSourceProperty) {
String publicKey = dataSourceProperty.getPublicKey();
if (StringUtils.hasText(publicKey)) {
dataSourceProperty.setUrl(decrypt(dataSourceProperty.getUrl()));
dataSourceProperty.setUsername(decrypt( dataSourceProperty.getUsername()));
dataSourceProperty.setPassword(decrypt(dataSourceProperty.getPassword()));
}
}
@Override
public void afterCreate(DataSource dataSource) {
}
/**
* 字符串解密
*/
private String decrypt(String cipherText) {
if (StringUtils.hasText(cipherText)) {
Matcher matcher = ENC_PATTERN.matcher(cipherText);
if (matcher.find()) {
try {
return CcmUtils.decrypt(matcher.group(1));
} catch (Exception e) {
log.error("DynamicDataSourceProperties.decrypt error ", e);
}
}
}
return cipherText;
}
}
```
修改配置文件,如下所示:
```yml
server:
port: 80
spring:
datasource:
dynamic:
datasource:
xiangjiao:
url: jdbc:mysql://xxxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: CCM(3914D177814A01A1)
password: CCM(3914D177814A01A1)
driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
primary: xiangjiao # 主连接
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
```
重启项目,访问请求测试能否查询到对应的数据:
