1.pom文件引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
2.在application.yml中增加配置
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
3.生成加密后的密文
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt
textEncryptor.setPassword("123456");
//要加密的数据(数据库的用户名或密码)
String url = textEncryptor.encrypt("XXX");
String username = textEncryptor.encrypt("XXX");
String password = textEncryptor.encrypt("XXX");
System.out.println("url:"+url);
System.out.println("username:"+username);
System.out.println("password:"+password);
}
4.替换掉原来的数据库配置
jdbcUrl: ENC(XXX)
username: ENC(XXX)
password: ENC(XXX)
5.启动项目时增加配置
在idea中启动增加环境配置 -Djasypt.encryptor.password=123456
在服务器上通过java -jar方式启动,增加--jasypt.encryptor.password=123456
docker方式启动,修改Dockerfile文件,增加"-Djasypt.encryptor.password=123456"