springboot integrates jasypt-spring-boot-starter to realize configuration file encryption

There will be some sensitive information in the configuration file, such as database account number and password. If it is not safe to use plain text, we can use jasypt to encrypt this information.

1. Introduce dependencies:

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

Pay attention to the version, some versions cannot be decrypted.

2. Encryption

public class EncryptConfigUtil {
    
    
    public static void main(String[] args) {
    
    

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword("12321");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("123");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }
}

insert image description here

3. Replace plaintext with ciphertext

insert image description here

insert image description here

4. Configure jaspyt

insert image description here
insert image description here
You can also configure the encryption algorithm, etc., so I won’t go into details here

5. Set the key in the JVM startup parameters

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/worilb/article/details/120590162