商城首页欢迎来到中国正版软件门户

您的位置:首页 >SpringBoot项目中如何利用application.yml文件配置数据库密码加密

SpringBoot项目中如何利用application.yml文件配置数据库密码加密

  发布于2023-05-12 阅读(0)

扫一扫,手机访问

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

SpringBoot项目中如何利用application.yml文件配置数据库密码加密

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

SpringBoot项目中如何利用application.yml文件配置数据库密码加密

编写加解密测试类:

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文件配置数据库密码加密

本文转载于:https://www.yisu.com/zixun/400852.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注