- 引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
- 加密相关代码
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Encrytor {
@Autowired
StringEncryptor encryptor;
@Test
public void getPass() {
//设置加密的盐
System.setProperty("jasypt.encryptor.password", "");
String url = encryptor.encrypt("jdbc:mysql://47.97.192.116:3306/sell?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8");
String name = encryptor.encrypt("132465");
String password = encryptor.encrypt("132456");
System.out.println(url + "----------------");
System.out.println(name + "----------------");
System.out.println(password + "----------------");
System.out.println(encryptor.decrypt("QXlVjlDyClggpdXxujkufQ=="));
Assert.assertTrue(name.length() > 0);
Assert.assertTrue(password.length() > 0);
}
}
3.配置文件使用
//ENC() 是固定方法
pring.datasource.url=ENC(加密后的字符串)
配置文件
jasypt.encryptor.password=任意值