在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件

记录:424

场景:在Spring Boot微服务,使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。

版本:JDK 1.8,Spring Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。

开源地址:https://github.com/ulisesbocchio/jasypt-spring-boot

1.在Spring Boot微服务使用jasypt-spring-boot-3.0.5版本

1.1在pom.xml引入依赖包

(1)依赖包

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

(2)解析

jasypt-spring-boot-3.0.5底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。

1.2在application.yml中添加jasypt配置

(1)配置

扫描二维码关注公众号,回复: 15134342 查看本文章

在application.yml中添加jasypt配置,主要是指定加密秘钥。

jasypt:
  encryptor:
    password: U3buwRJdQ2023

(2)解析

jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。

1.3在yml中配置已加密的信息

示例hub.example.password的值已经加密。

hub:
  example:
    password: ENC(C7KjxXpxXC/a/q1R8yCB+xkRIiHnDrDsmB8mEg3AWTvDNCf3nKiV09oZwHIS3SY9Sw1p3JfY3Ed7aWFEnVZ0rg==)

1.4在yml中配置已加密的信息存放格式

使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。

1.5启动微服务

微服务日志:

String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWITHHMACSHA512ANDAES_256
Encryptor config not found for property jasypt.encryptor.key-obtention-iterations, using default value: 1000
Encryptor config not found for property jasypt.encryptor.pool-size, using default value: 1
Encryptor config not found for property jasypt.encryptor.provider-name, using default value: null
Encryptor config not found for property jasypt.encryptor.provider-class-name, using default value: null
Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator
Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.RandomIvGenerator
Encryptor config not found for property jasypt.encryptor.string-output-type, using default value: base64

从日志中可以看出:

jasypt.encryptor.algorithm:PBEWITHHMACSHA512ANDAES_256。

jasypt.encryptor.salt-generator-classname: org.jasypt.salt.RandomSaltGenerator。

jasypt.encryptor.iv-generator-classname: org.jasypt.iv.RandomIvGenerator。

jasypt.encryptor.string-output-type: base64。

1.6测试

代码:

@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
  @Value("${hub.example.password}")
  private String cusPassword;
  @GetMapping("/load01")
  public Object load01() {
      log.info("测试开始...");
      log.info("从yml文件中获取hub.example.password=" + cusPassword);
      log.info("测试结束...");
      return "执行成功";
  }
}

解析:

使用注解获取的@Value("${hub.example.password}")属性值,是已经解密后的内容。

2.在Spring Boot微服务使用jasypt-spring-boot-2.1.2版本

2.1在pom.xml引入依赖包

(1)依赖包

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

(2)解析

jasypt-spring-boot-2.1.2底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。

2.2在application.yml中添加jasypt配置

(1)配置

在application.yml中添加jasypt配置,主要是指定加密秘钥。

jasypt:
  encryptor:
    password: U3buwRJdQ2023

(2)解析

jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。

2.3在yml中配置已加密的信息

示例hub.example.password的值已经加密。

hub:
  example:
    password: ENC(/BxyrksOnj3U/HCwkRVySHRZs2s4eZveCVncPoCzHMI=)

2.4在yml中配置已加密的信息存放格式

使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。

2.5启动微服务

微服务日志:

String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWithMD5AndDES
Encryptor config not found for property jasypt.encryptor.key-obtention-iterations, using default value: 1000
Encryptor config not found for property jasypt.encryptor.pool-size, using default value: 1
Encryptor config not found for property jasypt.encryptor.provider-name, using default value: null
Encryptor config not found for property jasypt.encryptor.provider-class-name, using default value: null
Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator
Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.NoIvGenerator
Encryptor config not found for property jasypt.encryptor.string-output-type, using default value: base64

从日志中可以看出:

jasypt.encryptor.algorithm:PBEWithMD5AndDES。

jasypt.encryptor.salt-generator-classname: org.jasypt.salt.RandomSaltGenerator。

jasypt.encryptor.iv-generator-classname: org.jasypt.iv.NoIvGenerator。

jasypt.encryptor.string-output-type: base64。

2.6测试

代码:

@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
  @Value("${hub.example.password}")
  private String cusPassword;
  @GetMapping("/load01")
  public Object load01() {
      log.info("测试开始...");
      log.info("从yml文件中获取hub.example.password=" + cusPassword);
      log.info("测试结束...");
      return "执行成功";
  }
}

解析:

使用注解获取的@Value("${hub.example.password}")属性值,是已经解密后的内容。

3.生成加密配置

在yml文件中配置加密属性时,需先生成加密配置信息,并使用ECN()包裹起来放入yml配置中。

public class JasyptDemo {
   public static void main(String[] args) {
     f1_BasicTextEncryptor();
     f2_AES256TextEncryptor();
   }
  /**
   * 加密工具类: org.jasypt.util.text.BasicTextEncryptor
   * 加密算法: PBEWithMD5AndDES
   */
  public static void f1_BasicTextEncryptor() {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    System.out.println("当前加密方式: 加密类: BasicTextEncryptor, 加密算法: PBEWithMD5AndDES ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
  }
  
  /**
   * 加密工具类: org.jasypt.util.text.AES256TextEncryptor
   * 加密算法: PBEWithHMACSHA512AndAES_256
   */
  public static void f2_AES256TextEncryptor() {
    AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
    System.out.println("当前加密方式: 加密类: AES256TextEncryptor, 加密算法: PBEWithHMACSHA512AndAES_256 ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
}
}

4.jasypt-spring-boot的版本2.1.2和3.0.5比对

4.1相同点

底层都使用jasypt-1.9.3。

4.2不同点

默认使用算法不同。

jasypt-spring-boot-2.1.2默认加密算法:PBEWithMD5AndDES。

jasypt-spring-boot-3.0.5默认加密算法:PBEWithHMACSHA512AndAES_256。

因此,从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,需要做适当修改。比如重新生成加密配置信息。或者指定算法。

5.jasypt-spring-boot的版本从2.1.2切换到3.0.5注意事项

从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,在不修改版2.1.2生成的加密信息时,则需在yml中指定算法信息。

报错:

Unable to decrypt property: ... Decryption of Properties failed,  make sure encryption/decryption passwords match

原因:

jasypt-spring-boot-2.1.2和jasypt-spring-boot-3.0.5默认使用加密和解密算法不一样,因此在使用jasypt-spring-boot-2.1.2时,生成的加密信息,切换到jasypt-spring-boot-3.0.5无法解密。

解决:

使用jasypt-spring-boot-3.0.5时,在yml文件中指定算法,不使用默认算法就行。

jasypt:
  encryptor:
    password: U3buwRJdQ2023
	algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

以上,感谢。

2023年4月27日

猜你喜欢

转载自blog.csdn.net/zhangbeizhen18/article/details/130416430