1. 引入依赖jar包
这个jar是一个人基于 jasypt 进行封装的starter
主要的作用是 实现敏感信息的加密
<!--springboot加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2. 使用
使用起来非常简单
- 在springboot的配置文件中加入
#springboot 加密所需要的key
jasypt.encryptor.password=abc
在真实项目中可以使用启动参数的方式
java -jar -Djasypt.encryptor.password=abc xxx.jar
也可以将这段配置在配置中心中 (例如:config或者nacos)- 在测试类中书写以下代码即可
@Autowired
StringEncryptor stringEncryptor;
@Test
void contextLoads() {
// 进行加密
String root = stringEncryptor.encrypt("加密的内容");
}
2.2 在配置文件中使用
spring.datasource.username=ENC(加密的用户名)
spring.datasource.password=ENC(加密的密码)