从0开始搭建一个生产级SpringBoot2.0.X项目(十四)SpringBoot数据库加密jasypt 3.0.4版本
一、pom文件新增依赖
<!--数据库加密--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
二、application 增加jasypt 配置
jasypt: encryptor: # 指定加密方式 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator
三、创建加密工具类生成加密后的密文
package com.sunreal.cpicapi.util;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/**
* 加解密工具类
*/
public class JasyptUtil {
/**
* 生成密文
*
* @param password 加密盐值
* @param value 加密的密码值
* @return
*/
public static String encyptPwd(String password, String value) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
String result = encryptor.encrypt(value);
return result;
}
/**
* 解密
*
* @param password 加密盐值
* @param value 解密密文
* @return
*/
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("PBEWithMD5AndDES");
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) {
String slat = "slat";
// 加密
String encPwd = encyptPwd(slat, "oraclepassword");
// 解密
String decPwd = decyptPwd(slat, encPwd);
System.out.println(encPwd);
System.out.println(decPwd);
}
}
四、替换数据库配置密码为加密后的密码
password: ENC(fYDWKW+Z95QBWbk82H3KdjumcA0f8nRE)
五、启动命令增
java -Djasypt.encryptor.password=slat -jar **.jar
原文地址:https://blog.csdn.net/weixin_41647530/article/details/145277687
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!