spring boot 项目如何使用jasypt加密
1.在pom.xml文件引入
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.5</version>
</dependency>
2.配置文件:src/main/resources/application.properties
3.启动文件:src/main/java/cn/liufuwen/ytxg/YtxgApplication.java 引入
4.编写工具类
package cn.liufuwen.ytxg.util;import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.iv.RandomIvGenerator;import java.io.IOException;
/*** @Description:加密/解密密码类, 来将我们要加密的密码进行加密 成加密串,然后将将加密串配置在配置文件中**/
public class JasyptUtil {public static void main(String[] args) throws InterruptedException, IOException {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 设置加密因子(与配置文件中的 password 一致)encryptor.setPassword("3b443abd993efc123a3abb1f29f05b");// 设置加密算法(与配置文件中的 algorithm 一致)encryptor.setAlgorithm("PBEWithHmacSHA512AndAES_128");encryptor.setIvGenerator(new RandomIvGenerator()); // 需要显式设置IV生成器// 解密
// String encrypteStr = "aA5VqEfdrho5GGbVQFWn1b3KxFRhNKJZCiSfIuqinwHa2mJ5A6W3ZvXS9O8Dsp5d";
// String decrypted = encryptor.decrypt(encrypteStr);
// System.out.println("解密后 : " + decrypted);//加密字符串String encodeStr="AaBb#2025";System.out.println("加密后 : " + encryptor.encrypt(encodeStr));}
}
5.将加密后的密文配置在配置文件上 , 需要使用ENC()包裹起来
spring.datasource.druid.password=ENC(tYADQ+kloNkO6FLSOPpPjZzfuNGnxdgv36bFeSL41tUy+JCYIACVIHJ9WWbEbzzl)