说明
使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些。
打开application.properties或application.yml,比如 MySql登陆密码,Redis登陆密码以及第三方的密钥等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。
jasypt由一个国外大神写了一个springboot下的工具包,用来加密配置文件中的信息。
GitHub Demo地址
https://github.com/jeikerxiao/spring-boot2/tree/master/spring-boot-encrypt
数据用户名和数据库密码加密为例 1. 引入包
查看最新版本可以到:
com.github.ulisesbocchiogroupId>
jasypt-spring-boot-starterartifactId>
2.1.0version>
dependency>
2. 配置加/解的密码 # jasypt加密的密匙
jasypt:
encryptor:
password: Y6M9fAJQdU7jNp5MW
3. 测试用例中生成加密后的秘钥 @RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseTest {
@Autowired
private StringEncryptor encryptor;
@Test
public void getPass() {
String url = encryptor.encrypt(“jdbc:mysql://localhost:33