作者:dmcm0006 | 来源:互联网 | 2023-09-18 07:56
目录
1.依赖
2.加密工具类
3.配置
1.依赖
com.github.ulisesbocchiojasypt-spring-boot-starter2.1.0
2.加密工具类
工具类代码
package cn.telecom.generation;import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Autowired;public class DBPasswordGenerator {@AutowiredStringEncryptor stringEncryptor;public static void main(String[] args) {String key="SystemKey";//加密盐值String password="123456";//需要加密的密码明文PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();encryptor.setConfig(cryptor(key));String result = encryptor.encrypt(password);System.out.println("秘文:"+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;}}
3.配置
如上,就已经生效了,在代码中就不存在明文的数据库密码了。