SpringBoot项目中如何利用application.yml文件配置数据库密码加密
更新:HHH   时间:2023-1-7


这篇文章主要讲解了“SpringBoot项目中如何利用application.yml文件配置数据库密码加密”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot项目中如何利用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文件配置数据库密码加密这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!

返回移动开发教程...