使用该依赖库
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
使用下面的代码进行加密数据库配置信息
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐)
textEncryptor.setPassword("G0CsffvDz7oFJsfJn6");
//要加密的数据(数据库的用户名或密码)
String url = textEncryptor.encrypt("jdbc:mysql://127.0.0.1:60394/test?characterEncoding=UTF-8&useSSL=true");
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("S123455!");
System.out.println("url:"+url);
System.out.println("username:"+username);
System.out.println("password:"+password);
将配置文件中的以下内容进行替换, 注意是 ENC ,不是ENV
spring.datasource.url=ENC(2kwi32FUIhwrFAJfhwefwtgFgaw==)
spring.datasource.username=ENC(54po6dV02MF5CHAfisfygAinv/N0I)
spring.datasource.password=ENC(kBXACijveapCMgsefhOFRa7UY=)
最后再启动类上添加注解
@EnableEncryptableProperties
启动jar包时,添加加密时所需的盐参数
java -jar -Djasypt.encryptor.password=G0CsffvDz7oFJsfJn6 xxx.jar