目录
1.依赖
<!-- 数据库密码加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
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 {
@Autowired
StringEncryptor 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.配置
如上,就已经生效了,在代码中就不存在明文的数据库密码了。