Skip to content

Commit 68839a6

Browse files
authored
[DOC]新增MySQL密码以加密方式存储并使用的文档 (#1135)
1 parent e2692a6 commit 68839a6

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## YML文件MYSQL密码加密存储手册
2+
3+
### 1、本地部署加密
4+
5+
**第一步:生成密文**
6+
7+
在本地仓库中找到jasypt-1.9.3.jar,默认在org/jasypt/jasypt/1.9.3中,使用`java -cp`生成密文。
8+
9+
```bash
10+
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=mysql密码 password=加密的salt algorithm=PBEWithMD5AndDES
11+
```
12+
13+
```bash
14+
## 得到密文
15+
DYbVDLg5D0WRcJSCUGWjiw==
16+
```
17+
18+
**第二步:配置jasypt**
19+
20+
在YML文件中配置jasypt,例如
21+
22+
```yaml
23+
jasypt:
24+
encryptor:
25+
algorithm: PBEWithMD5AndDES
26+
iv-generator-classname: org.jasypt.iv.NoIvGenerator
27+
```
28+
29+
**第三步:配置密文**
30+
31+
使用密文替换YML文件中的明文密码为ENC(密文),例如[application.yml](https://github.com/didi/KnowStreaming/blob/master/km-rest/src/main/resources/application.yml)中MYSQL密码。
32+
33+
```yaml
34+
know-streaming:
35+
username: root
36+
password: ENC(DYbVDLg5D0WRcJSCUGWjiw==)
37+
```
38+
39+
**第四步:配置加密的salt(选择其一)**
40+
41+
- 配置在YML文件中(不推荐)
42+
43+
```yaml
44+
jasypt:
45+
encryptor:
46+
password: salt
47+
```
48+
49+
- 配置程序启动时的命令行参数
50+
51+
```bash
52+
java -jar xxx.jar --jasypt.encryptor.password=salt
53+
```
54+
55+
- 配置程序启动时的环境变量
56+
57+
```bash
58+
export JASYPT_PASSWORD=salt
59+
java -jar xxx.jar --jasypt.encryptor.password=${JASYPT_PASSWORD}
60+
```
61+
62+
## 2、容器部署加密
63+
64+
利用docker swarm 提供的 secret 机制加密存储密码,使用docker swarm来管理密码。
65+
66+
### 2.1、secret加密存储
67+
68+
**第一步:初始化docker swarm**
69+
70+
```bash
71+
docker swarm init
72+
```
73+
74+
**第二步:创建密钥**
75+
76+
```bash
77+
echo "admin2022_" | docker secret create mysql_password -
78+
79+
# 输出密钥
80+
f964wi4gg946hu78quxsh2ge9
81+
```
82+
83+
**第三步:使用密钥**
84+
85+
```yaml
86+
# mysql用户密码
87+
SERVER_MYSQL_USER: root
88+
SERVER_MYSQL_PASSWORD: mysql_password
89+
90+
knowstreaming-mysql:
91+
# root 用户密码
92+
MYSQL_ROOT_PASSWORD: mysql_password
93+
secrets:
94+
mysql_password:
95+
external: true
96+
```
97+
98+
### 2.2、使用密钥文件加密
99+
100+
**第一步:创建密钥**
101+
102+
```bash
103+
echo "admin2022_" > password
104+
```
105+
106+
**第二步:使用密钥**
107+
108+
```yaml
109+
# mysql用户密码
110+
SERVER_MYSQL_USER: root
111+
SERVER_MYSQL_PASSWORD: mysql_password
112+
secrets:
113+
mysql_password:
114+
file: ./password
115+
```

km-rest/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
<version>${springboot.version}</version>
139139
</dependency>
140140

141+
<dependency>
142+
<groupId>com.github.ulisesbocchio</groupId>
143+
<artifactId>jasypt-spring-boot-starter</artifactId>
144+
<version>3.0.5</version>
145+
</dependency>
146+
141147
<!--testcontainers-->
142148
<dependency>
143149
<groupId>org.testcontainers</groupId>

0 commit comments

Comments
 (0)