1.在pom.xml文件中添加依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.17</version>
</dependency>
github地址:https://github.com/ulisesbocchio/jasypt-spring-boot
2.在測試類中加密明文
import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;
public class TestYmlEncryption {
@Test
public void jasyptTest() {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
// application.yml配置的jasypt.encryptor.password
encryptor.setPassword("MS");
// 對root進行加密操作
System.out.println(encryptor.encrypt("mq"));
// 進行解密操作
System.out.println(encryptor.decrypt("***"));
}
}
3.在application.yml中配置加密信息
jasypt:
encryptor:
password: MS
username: ENC(加密後祕鑰)