配置文件用jasypt加密
1.在SpringBoot中,通过jasypt可以进行加密解密. 这个是双向的, 且可以配置密钥.
2.添加依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>
3.创建工具包
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;
public class UtilTests {
@Test
public void jasyptTest() {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
// application.properties, jasypt.encryptor.password
encryptor.setPassword("test");
// encrypt root
System.out.println(encryptor.encrypt("jdbc:mysql://${MYSQL_HOST:127.0.0.1}:${MYSQL_PORT:3306}/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai"));
System.out.println(encryptor.encrypt("root"));
System.out.println(encryptor.encrypt("root123"));
// decrypt, the result is root
System.out.println(encryptor.decrypt("lzPf+Oxavi70usXWsLKOJGeXelE1uge8vLwsgWiSSkfpftZPNiSmVK5xVk8qozNRwMfaax3zfocABNNhQB0hQqt+vyjYOaGdMzmP07xuHmDoWapuZZMhWQa6d6w4OVX3Z/JMnm0V2NGkYkScOj0IpdU+fEwSxhbAThIh/wR/HddyZSGxEV9TXZet27Pegw+gM6I9hzFvBY4dsXQqS1q5JmWUWVg4IrclQS4UpOoREIgrfJjfqy3v5HSAMT1b2ayGTwytc+sKcMj9XP8tnf2dtw=="));
System.out.println(encryptor.decrypt("vyyJZDny2DKH91TMycXsng=="));
System.out.println(encryptor.decrypt("c6Q5EEP1lZi3YdW3h62gpA=="));
}
}
4.使用
spring:
datasource:
jdbc-url: ENC(lzPf+Oxavi70usXWsLKOJGeXelE1uge8vLwsgWiSSkfpftZPNiSmVK5xVk8qozNRwMfaax3zfocABNNhQB0hQqt+vyjYOaGdMzmP07xuHmDoWapuZZMhWQa6d6w4OVX3Z/JMnm0V2NGkYkScOj0IpdU+fEwSxhbAThIh/wR/HddyZSGxEV9TXZet27Pegw+gM6I9hzFvBY4dsXQqS1q5JmWUWVg4IrclQS4UpOoREIgrfJjfqy3v5HSAMT1b2ayGTwytc+sKcMj9XP8tnf2dtw==)
username: ENC(vyyJZDny2DKH91TMycXsng==)
password: ENC(c6Q5EEP1lZi3YdW3h62gpA==)
driver-class-name: com.mysql.jdbc.Driver
continue-on-error: true