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.
Article directory
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);
}
}
3. Replace plaintext with ciphertext
4. Configure jaspyt
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