springboot配置文件敏感信息加密
最近项目在渗透测试和代码检查,配置文件中的敏感信息竟然要加密,由于CAS中很多配置都是默认配置自动加载,不容易写代码来加载属性,后来在github上找到一个对springboot配置文件进行加密解密的工具–jasypt
首先写在pom文件中引入jasypt的依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
然后在application中配置加密的密钥:
# 配置加密的密钥
jasypt.encryptor.password=kkk
# 在测试类中写入写一个加密的测试方法
```java
public class EncryptPasswdApplicationTests {
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String password="manager05";//要加密的密码
String result = stringEncryptor.encrypt(password);
//打印结果
System.out.println(result);
}
}
打印加密之后的密码:
对敏感信息进行加密
这里使用的是ldap来测试,如果加密后的密码可以连接上ldap则配置文件加密成功,将打印出来的密文密码粘贴到配置文件中的相应位置:
spring.ldap.urls=ldap://172.16.**.**:13601
spring.ldap.base=ou=User,dc=**,dc=com
spring.ldap.username=***
#spring.ldap.password=manager05
spring.ldap.password=ENC(/jZwF1GsriJL60XofakOia+jP0RLtq5O)
运行测试方法getSize()获取ldap中所有的用户数量,如果可以获取到数量则说明ldap连接成功。
在这里插入图片描述
运行之后可以发现已经获取到ldap的用户数量,则说明springboot中application.xml中敏感信息已经加密且在加载时解密。而且encrypt()加密