SpringBoot配置文件加密
1 引入Maven
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
2 添加配置
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES #算法-固定写法一般没人改
password: 1234 #密钥
iv-generator-classname: org.jasypt.iv.NoIvGenerator #设置初始向量IV生成器的类名
3 找到maven仓库下:将内容进行加密
4 CMD执行命令
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=1234 algorithm=PBEWithMD5AndDES
java -cp jasypt-版本根据自己的修改.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="明文" password=密钥 algorithm=算法 这三个值要和配置文件里一样
5 代码加密
public static void main(String[] args) {
String ALGORITHM_INFO = "PBEWithMD5AndDES";
final String PASSWORD_INFO = "qwert12345";
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
//配置文件中配置如下的算法
standardPBEStringEncryptor.setAlgorithm(ALGORITHM_INFO);
//配置文件中配置的password
standardPBEStringEncryptor.setPassword(PASSWORD_INFO);
//要加密的文本
String name = standardPBEStringEncryptor.encrypt("qaRwKNH5eslcaxyoPAc7IlylUPIhkZ");
//将加密的文本写到配置文件中
System.out.println("name=" + name);
}