Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-05-12 10:34:05
forward
2628 people have browsed it

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);
  }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template