配置文件加密存储

1.导入jasypt坐标

  • 添加jasypt依赖坐标

    pom.xml

1
2
3
4
5
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>

2.实现自定义配置文件加密算法

  • 实现接口PBEStringEncryptor

    com/gaomu/utils/crypto/SM4YmlEncryptor.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class SM4YmlEncryptor implements PBEStringEncryptor {

private String password;

//密钥key不可硬编码
private static String key = "";
private static String iv = "43374b4536657054564459466e774c53";

@Override
public void setPassword(String password) {
this.password = password;
}

@Override
public String encrypt(String message) {
String ciper = "";
try {
ciper = SM4Util.encryptCBC(message, password, iv);
} catch (Exception e) {
throw new RuntimeException(e);
}
return ciper;
}

@Override
public String decrypt(String encryptedMessage) {
String plaintext = "";
try {
plaintext = SM4Util.decryptCBC(encryptedMessage, password, iv);
} catch (Exception e) {
throw new RuntimeException(e);
}
return plaintext;
}
}

3.测试main方法

  • 可在main方法中生成ENC密文

    com/gaomu/utils/crypto/SM4YmlEncryptor.java

1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) {
//本项目默认sm4配置文件加密密钥为33356f733270766f7959655165374256
SM4YmlEncryptor smm4 = new SM4YmlEncryptor();
smm4.setPassword(key);
String originalText = "01ee3f7d8f10938428ed38654b0ed81fa8d352dc0146d4fddb2e034d636d2749";
String encryptedText = smm4.encrypt(originalText);
String decryptedText = smm4.decrypt(encryptedText);
System.out.println("Original text: " + originalText);
System.out.println("Encrypted text: " + encryptedText);
System.out.println("decrypted Text: " + decryptedText);
}

4.添加自定义加密算法配置

  • 在springboot配置中注入Bean

    com/gaomu/config/AppConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class AppConfig {
//密钥从环境变量中获取,而非配置文件
@Value("${JASYPT_SM4_PASSWORD:defaultValue}")
private String jasyptSM4Password;


@Bean("jasyptStringEncryptor")
public PBEStringEncryptor stringEncryptor() {
SM4YmlEncryptor sm4YmlEncryptor = new SM4YmlEncryptor();
sm4YmlEncryptor.setPassword(jasyptSM4Password); // 设置你的加密密钥
return sm4YmlEncryptor;
}
}

5.添加解密配置文件密钥

5.1.IDEA配置环境变量

  • 如果是在IDEA中系统需要在IDEA启动配置中添加环境变量

  • 添加如图添加环境变量配置

image-20231230125028380

  • 如果没有环境变量选项需要在修改选择中添加该选项

image-20231230125146612

image-20231230125204335

5.2.系统配置环境变量

  • 如果是在Windows生产环境中需要在系统环境变量中配置

image-20231230125400177

5.3.如果是在linux中配置环境变量

1
2
3
4
# 添加环境变量
$ echo "export JASYPT_SM4_PASSWORD=33356f733270766f7959655165374256" >> ~/.bashrc
# 重载环境变量
$ source ~/.bashrc

6.配置application.yml

  • 使其生效还需要在springboot配置文件中配置

    application.yml

1
2
3
4
5
6
7
8
jasypt:
encryptor:
password:
algorithm: jasyptStringEncryptor
stringOutputType: UTF-8
# password不用配置,因为我们已经在环境变量中配置完成,
# algorithm配置自定义的加密算法
# 配置输出编码

7.配置加密信息

7.1.在main方法中生成数据库密码密文

image-20231230130435692

7.2.在application.yml配置文件添加加密后的密文

  • 密文需要使用ENC(cipher)标识,如此springboot启动配置才会解密该数据
1
2
3
4
5
6
spring:
datasource:
url: jdbc:mysql://localhost:3306/auth_user?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: ENC(i4+qvUO1mKlzmIlkB++s3Q==)
driver-class-name: com.mysql.cj.jdbc.Driver