目录
简介:
Jasypt是一个 加密库, Github上有一个集成了 Jasypt的 Spring Boot库,叫 jasypt-spring-boot
-
依赖导入:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
-
yaml文件配置:
# 加密的密钥# 测试环境可以采用在配置文件中配置# 生产环境建议采用启动参数的形式传入# 其他配置参数参考:com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationPropertiesjasypt.encryptor.password=you saltalgorithm: 算法# 解密得到原始密码spring.datasource.password= ENC(密文)
例如:
jasypt:
encryptor:
password: 你自己所用的盐,可以任意给
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
-
密文生成方法:
package com.mastercard.gx.gamerxchange.util;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
public class JasyptTool {
public static void main(String[] args) {
StandardPBEStringEncryptor se = new StandardPBEStringEncryptor();
/加密所需的salt(盐)
se.setPassword("xXXXXX");
//算法
se.setAlgorithm("PBEWithMD5AndDES");
System.out.println(se.encrypt("明文密码")); //加密
System.out.println(se.decrypt("密文")); //mq密码解密
}
}
-
密文添加到配置中:
ENC(加密后的密文)
提示:
3.0.3之后的版本,配置文件中必须增加(iv-generator-classname: org.jasypt.iv.NoIvGenerator),之前可以不加。否则解密失败报错:
Failed to bind properties under ‘spring.datasource.password‘ to java.lang.String: Reason: Fail