Springboot 项目对配置文件使用jasypt加密
加密的目的在于安全性,否则有心人反编译你的项目后,拿到你的配置文件,你没有加密,获取到你数据库的账号密码.
配置文件中添加:
#加密的盐
jasypt.encryptor.password:yan
建立main方法,或者测试类对你的数据库,redis账号密码进行加密
@Test
public void jasyptTest(){
BasicTextEncryptor encryptor = new BasicTextEncryptor();
//配置文件中加密的盐
encryptor.setPassword("yan");
//账号
System.out.println(encryptor.encrypt("root"));
//密码
System.out.println(encryptor.encrypt("root"));
}
打印出来是:
SMZ+qN+4u965ME2dwarUSg==
QHRGcpI4YUtB6a6VQCpIFg==
配置文件中使用:
spring.datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/xueluo?useUnicode=true
username: ENC(SMZ+qN+4u965ME2dwarUSg==)
password: ENC(QHRGcpI4YUtB6a6VQCpIFg==)
注意:
pom文件也需要进行配置哦:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
主启动类配置注解:
@EnableEncryptableProperties
齐活儿了~~
想验证加密正确性,可以使用
System.out.println(encryptor.decrypt("SMZ+qN+4u965ME2dwarUSg=="));