夜间模式
配置加密
给你的配置信息加密(账号、密码等等)
以账号密码为例
先生成账号密码的密文
Java
package com.itheima.mp;
import com.baomidou.mybatisplus.core.toolkit.AES;
import org.junit.jupiter.api.Test;
class MpDemoApplicationTests {
@Test
void contextLoads() {
// 生成 16 位随机 AES 密钥
String randomKey = AES.generateRandomKey();
System.out.println("randomKey = " + randomKey);
// 利用密钥对用户名加密
String username = AES.encrypt("root", randomKey);
System.out.println("username = " + username);
// 利用密钥对用户名加密
String password = AES.encrypt("201819", randomKey);
System.out.println("password = " + password);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SQL
randomKey = 8930b163173240cb
username = IX9AH7q/j+xdgTunSzrhzg==
password = mUs0x0cIXwhx9lYudVvDfw==
1
2
3
2
3
YAML
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: mpw:IX9AH7q/j+xdgTunSzrhzg== # 密文要以 mpw:开头
password: mpw:mUs0x0cIXwhx9lYudVvDfw== # 密文要以 mpw:开头
1
2
3
4
5
6
2
3
4
5
6
在启动项目的时候,需要把刚才生成的秘钥添加到启动参数中,像这样:
--mpw.key=8930b163173240cb
参数结果正常:
TIP
在测试类中无法添加启动参数,需要加到测试类的注解上