添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在 SpringBoot 项目中,我们经常会在 application.yml 配置文件中直接写上数据库的明文密码,这不是一个很好的方式,一旦代码泄漏,后果不堪设想。因此在这里提供一个将明文密码配置成加密密码的方法,供大家参考。

Step 1

首先导入加密工具的依赖包:

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

注意:当依赖版本为 3.0.3 时,在 spring 的 application.yml 文件中需多添加一行配置:

1
2
3
jasypt:
encryptor:
iv-generator-classname: org.jasypt.iv.NoIvGenerator

Step 2

编写加密解密的工具类,用于获取对明文加密后的字符串:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @ClassName JasyptUtil
* @Description
* @Author Dingcai Ge
* @Date 2021/9/2 16:05
* @Version 1.0
**/
public class JasyptUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(JasyptUtil.class);

/** 加密的算法,这个算法是默认的 */
private static final String ALGORITHM_INFO = "PBEWithMD5AndDES";
/** 加密的密钥 */
private static final String SECRET_KEY = "QAZWSXEDCRNGTEST";

/**
* 加密密码
* @param plainText 明文字符串
* @return
*/
public static String getEncryptedText(String plainText) {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
config.setAlgorithm(ALGORITHM_INFO);
config.setPassword(SECRET_KEY);
standardPBEStringEncryptor.setConfig(config);
return standardPBEStringEncryptor.encrypt(plainText);
}

/**
* 解密密码
* @param encryptedText 加密串
* @return
*/
public static String getPlainText(String encryptedText) {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
config.setAlgorithm(ALGORITHM_INFO);
config.setPassword(SECRET_KEY);
standardPBEStringEncryptor.setConfig(config);
return standardPBEStringEncryptor.decrypt(encryptedText);
}
}

用加密方法,传入密码的明文字符串,获取到加密后的密文串:

1
2
3
4
5
6
public class Test {

public static void main(String[] args) {
System.out.println(JasyptUtil.getEncryptedText("lalala"));
}
}

Step 3

最后在 application.yml 文件中添加配置:

1
2
3
4
5
jasypt:
encryptor:
password: QAZWSXEDCRNGTEST
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator

并将密文串放在 ENC() 的括号中,替换原先的明文密码即可:

1
2
3
4
5
spring:
datasource:
url:
username: ENC(umrbBLJQOAswd3z9JOqQnA==)
password: ENC(se5fkx6dCNxWVq/iO5LKqA==)

温馨提示:若项目启动失败,可尝试在 Spring Boot 启动类上加上 @EnableEncryptableProperties 注解。

GitHub 项目地址: https://github.com/ulisesbocchio/jasypt-spring-boot

缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save

3、在根目录_config.yml里添加配置: jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true