jasypt-spring-boot-starter项目如何使用jasypt加密密码
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.iv.RandomIvGenerator;
import org.jasypt.salt.RandomSaltGenerator;
/**
* 加密密码的工具
*
* @author xxx
* @since 2025-03-17
*/
public class JasyptTest {
public static void main(String[] args) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
// 设置加密密钥,这里的密钥应该和你配置文件中的jasypt.encryptor.password一致
encryptor.setPassword("xxxx");
encryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
encryptor.setIvGenerator(new RandomIvGenerator());
encryptor.setStringOutputType("base64");
encryptor.setSaltGenerator(new RandomSaltGenerator());
encryptor.setKeyObtentionIterations(1000);
String encryptedText = encryptor.encrypt("app@2022");
System.out.println("加密后的密码: \n".concat("ENC(").concat(encryptedText).concat(")"));
}
}
这些参数都是加密算法必须的,如果配置中只指定了加密密码,类似下面这样:
jasypt:
encryptor:
password: xxx
那么其他参数就需要去框架类(com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties)中找默认值.
注意:加密后的密码要通过ENC()包起来