第一步:maven引jar包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>1.14</version>
</dependency>
第二步:配置文件
#jasypt加密的盐值
jasypt.encryptor.password=erp
第三步:调用加密方法
public class JasyptTest { @Autowired
static StringEncryptor encryptor;
//加密
public static void getPass(){
encryptor = SpringUtil.getBean(StringEncryptor.class);
String name = encryptor.encrypt("root");
String password = encryptor.encrypt("firewall_new");
System.out.println(name); //hifVDwDPW5MNYa0tAabvJQ==
System.out.println(password); //5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi
}
//解密
public static void passDecrypt(){
encryptor = SpringUtil.getBean(StringEncryptor.class);
String username = encryptor.decrypt("hifVDwDPW5MNYa0tAabvJQ==");
String password = encryptor.decrypt("5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi");
System.out.println(username+"--"+password);
} }
控制台就会输出加密后的秘钥。
切记:将上面的生成的密匙如下替换,此处主要是数据库密码密文使用ENC进行标识