Home  >  Article  >  Java  >  How to use the application.yml file to configure database password encryption in the SpringBoot project

How to use the application.yml file to configure database password encryption in the SpringBoot project

WBOY
WBOYforward
2023-05-12 10:34:052575browse

To use the @SpringBootApplication annotation to start the project, you only need to add the maven dependency

How to use the application.yml file to configure database password encryption in the SpringBoot project

We use this jar package to encrypt and decrypt information:

How to use the application.yml file to configure database password encryption in the SpringBoot project

Write encryption and decryption test class:

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);
  }
}

We got the encryption string, now let’s modify the configuration of application.yml:

Let’s encrypt Just put the string in ENC ({encrypted string}).

How to use the application.yml file to configure database password encryption in the SpringBoot project

You need to configure the secret key during startup

Add the secret key to the startup parameters

How to use the application.yml file to configure database password encryption in the SpringBoot project

How to use the application.yml file to configure database password encryption in the SpringBoot project

The above is the detailed content of How to use the application.yml file to configure database password encryption in the SpringBoot project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete