工具类springboot将配置文件中的数据加密
一、引入依赖
implementation "com.github.ulisesbocchio:jasypt-spring-boot-starter:2.1.0"
二、在配置文件中加入盐的配置
jasypt.encryptor.password: e9fbdb234534
三、将想要加密的信息加密
使用该工具类中的main方法可以获得加密后的密码串
package utils;import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class JasyptUtils {
public static String encryptPwd(String password, String value) {
PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
encryptOr.setConfig(cryptOr(password));
String result = encryptOr.encrypt(value);
return result;
}
public static String decyptPwd(String password, String value) {
PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
encryptOr.setConfig(cryptOr(password));
String result = encryptOr.decrypt(value);
return result;
}
public static SimpleStringPBEConfig cryptOr(String password) {
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args) {
// 加密
System.out.println(encryptPwd("e9fbdb234534", "ZZB@sales_user_123!@#"));
// 解密
// System.out.println(decyptPwd("e9fbdb2d3b21", "dcIXZ2hXkcuzbVwTjgOFoiYbeNtVUMtF9uzZZG7SQ0k="));
}
}
以上是 工具类springboot将配置文件中的数据加密 的全部内容, 来源链接: utcz.com/z/510282.html