1、在所在的pom文件中增加依赖
<!--数据库密码加密 jasypt -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2、新建JasyptUtils工具类、生成在配置文件中填写的密文
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
/**
* 数据库密码加密 工具类 生成加密后的账户、密码*/
public class JasyptUtils {
public static void main(String[] arg){
StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
/*配置文件中algorithm对应的算法名:PBEWithMD5AndDES*/
standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
/*配置文件中password 对应的值(这个值自己随便定义)*/
standardPBEStringEncryptor.setPassword("ZIJIDINGYI");
/*获得name、password的加密文本*/
String name = standardPBEStringEncryptor.encrypt("root"); //账户
String password =standardPBEStringEncryptor.encrypt("123456"); //密码
/*以下打印的就是在配置文件中需要配置的密文*/
System.out.println("name:"+name);
System.out.println("password:"+password);
}
}
3、配置文件yml
#新增 jasypt 配置 start
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES
password: ZIJIDINGYI #步骤2中 standardPBEStringEncryptor.setPassword的值一致
#新增 jasypt 配置 end
#修改数据源配置的username、password值
.......
url:
username: ENC(步骤2 name 生成的密文)
password: ENC(步骤2 password 生成的密文)
4、测试验证

该博客介绍了如何在Spring Boot应用中使用Jasypt进行数据库密码加密。首先,在pom.xml文件中添加Jasypt依赖,然后创建JasyptUtils工具类,利用StandardPBEStringEncryptor生成加密的用户名和密码。接着,在配置文件yml中配置Jasypt的algorithm和password。最后,将数据源配置的username和password替换为加密后的值。通过这种方式,可以增强应用的安全性。
1033

被折叠的 条评论
为什么被折叠?



