pom文件引入
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
密文生成代码
/**
* jasypt-spring-boot-starter 生成密文的工具代码
*/
public class EncryptConfigUtil {
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt
textEncryptor.setPassword("123456");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("123456");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
配置文件中密文
db:
mysql:
druid:
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driverClassName: com.mysql.cj.jdbc.Driver
username: ENC(opGC8sQt5JVH3j4VD5i2Ug)
password: ENC(r6YUrfKMAAzzltzmApEYv7lZyILULq==)
jasypt加密password配置
// 方式一:yml中(不安全)
jasypt.encryptor.password=123456
// 方式二:JVM启动参数中设置
-Djasypt.encryptor.password=123456