SpringBoot项目中如何对application.yml中的数据库密码进行加密配置?

2026-05-26 06:021阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计384个文字,预计阅读时间需要2分钟。

SpringBoot项目中如何对application.yml中的数据库密码进行加密配置?

在Spring Boot开发中,配置数据库连接信息通常在`application.yml`文件中完成。若在启动时未对数据库密码进行加密,则密码以明文形式存在,存在安全隐患。因此,建议对数据库密码进行加密处理。

在Spring boot开发中,需要在application.yml文件里配置数据库的连接信息,或者在启动时传入数据库密码,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了,因此需要进行加密处理才行。

使用@SpringBootApplication注解启动的项目,只需增加maven依赖

我们对信息加解密是使用这个jar包的:

编写加解密测试类:

package cn.linjk.ehome; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig; import org.junit.Test; public class JasyptTest { @Test public void testEncrypt() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); // 加密的算法,这个算法是默认的 config.setPassword("test"); // 加密的密钥 standardPBEStringEncryptor.setConfig(config); String plainText = "88888888"; String encryptedText = standardPBEStringEncryptor.encrypt(plainText); System.out.println(encryptedText); } @Test public void testDe() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); config.setPassword("test"); standardPBEStringEncryptor.setConfig(config); String encryptedText = "ip10XNIEfAMTGQLdqt87XnLRsshu0rf0"; String plainText = standardPBEStringEncryptor.decrypt(encryptedText); System.out.println(plainText); } }

加密串拿到了,现在来修改application.yml的配置:

我们把加密串放在ENC({加密串})即可。

SpringBoot项目中如何对application.yml中的数据库密码进行加密配置?

启动时需要配置 秘钥

将秘钥加入启动参数

到此这篇关于SpringBoot项目application.yml文件数据库配置密码加密的方法的文章就介绍到这了,更多相关SpringBoot application.yml数据库加密内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计384个文字,预计阅读时间需要2分钟。

SpringBoot项目中如何对application.yml中的数据库密码进行加密配置?

在Spring Boot开发中,配置数据库连接信息通常在`application.yml`文件中完成。若在启动时未对数据库密码进行加密,则密码以明文形式存在,存在安全隐患。因此,建议对数据库密码进行加密处理。

在Spring boot开发中,需要在application.yml文件里配置数据库的连接信息,或者在启动时传入数据库密码,如果不加密,传明文,数据库就直接暴露了,相当于"裸奔"了,因此需要进行加密处理才行。

使用@SpringBootApplication注解启动的项目,只需增加maven依赖

我们对信息加解密是使用这个jar包的:

编写加解密测试类:

package cn.linjk.ehome; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig; import org.junit.Test; public class JasyptTest { @Test public void testEncrypt() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); // 加密的算法,这个算法是默认的 config.setPassword("test"); // 加密的密钥 standardPBEStringEncryptor.setConfig(config); String plainText = "88888888"; String encryptedText = standardPBEStringEncryptor.encrypt(plainText); System.out.println(encryptedText); } @Test public void testDe() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); config.setPassword("test"); standardPBEStringEncryptor.setConfig(config); String encryptedText = "ip10XNIEfAMTGQLdqt87XnLRsshu0rf0"; String plainText = standardPBEStringEncryptor.decrypt(encryptedText); System.out.println(plainText); } }

加密串拿到了,现在来修改application.yml的配置:

我们把加密串放在ENC({加密串})即可。

SpringBoot项目中如何对application.yml中的数据库密码进行加密配置?

启动时需要配置 秘钥

将秘钥加入启动参数

到此这篇关于SpringBoot项目application.yml文件数据库配置密码加密的方法的文章就介绍到这了,更多相关SpringBoot application.yml数据库加密内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!