pom文件引入

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot-starter</artifactId>
  4. <version>2.1.0</version>
  5. </dependency>

密文生成代码

  1. /**
  2. * jasypt-spring-boot-starter 生成密文的工具代码
  3. */
  4. public class EncryptConfigUtil {
  5. public static void main(String[] args) {
  6. BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
  7. //加密所需的salt
  8. textEncryptor.setPassword("123456");
  9. //要加密的数据(数据库的用户名或密码)
  10. String username = textEncryptor.encrypt("root");
  11. String password = textEncryptor.encrypt("123456");
  12. System.out.println("username:"+username);
  13. System.out.println("password:"+password);
  14. }
  15. }

配置文件中密文

  1. db:
  2. mysql:
  3. druid:
  4. url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
  5. driverClassName: com.mysql.cj.jdbc.Driver
  6. username: ENC(opGC8sQt5JVH3j4VD5i2Ug)
  7. password: ENC(r6YUrfKMAAzzltzmApEYv7lZyILULq==)

jasypt加密password配置

  1. // 方式一:yml中(不安全)
  2. jasypt.encryptor.password=123456
  3. // 方式二:JVM启动参数中设置
  4. -Djasypt.encryptor.password=123456