springboot项目使用jasypt加密数据库密码
- 导入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
- application.yml设置
jasypt:
encryptor:
password: test
# password 为加密盐值,不可泄露
- 根据盐值获取加密结果 ```java 代码方式 BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(盐),自定义 textEncryptor.setPassword(“test”); //要加密的数据(数据库的用户名或密码) String username = textEncryptor.encrypt(“root”); System.out.println(“username:” + username);
//解密 String decrypt = textEncryptor.decrypt(“9N0j84xejjkUxmfM+MiaYB0sEfMZWDqY”); System.out.println(“username decrypt:” + decrypt);
运行jasypt的jar包方式 java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=”test” password=test algorithm=PBEWithMD5AndDES
input为你的明文密码,password为你的私钥,algorithm这个是一个规则,切勿更改
4.
application
datasource:
driverClassName: com.mysql.jdbc.Driver
username: ENC(x+RLfpIqGlHU5k/c6Q==)
password: ENC(9N0j84xYB0sEfMZWDqY)
**可以将盐值放入环境变量或者另存文件保证安全性,DEMO_HOME : test 记得重启**
1.
修改application
jasypt: encryptor: password: ${DEMO_HOME}
配置profile文件
export JASYPT_PASSWORD=pkslow
生效
source /etc/profile
运行java程序时
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar ```