gpt4 book ai didi

java - 将 Spring Boot 1.x 升级到 2.x(如果使用 {cipher} 文本,则更新 ENCRYPT KEY VM 参数)

转载 作者:行者123 更新时间:2023-11-30 05:26:13 25 4
gpt4 key购买 nike

如果您的 spring-boot 应用程序属性文件中使用了 {cipher} 加密文本。

application.ymlapplication.properties

my.password='{cipher}68e78a954bfa0297ecc733`

上面是 SpringBoot2 中的启动失败,并显示错误消息无法解密:key=my.password

堆栈跟踪

java.lang.IllegalStateException: Cannot decrypt: key=enterpriseInventoryService.password
at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:292)
at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.lambda$decrypt$0(EnvironmentDecryptApplicationInitializer.java:270)
at java.util.LinkedHashMap.replaceAll(Unknown Source)
at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:265)
at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.decrypt(EnvironmentDecryptApplicationInitializer.java:190)
at org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.initialize(EnvironmentDecryptApplicationInitializer.java:124)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener$DelegatingEnvironmentDecryptApplicationInitializer.initialize(BootstrapApplicationListener.java:413)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:623)
.
.
Caused by: java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:142)

最佳答案

Spring-boot-1

以下任何一个 VM 参数都可以有效提供 key ,以便 spring 在加载属性时可以解密'{cipher}f75146b2d391aa6'

  1. 加密 key (默认 key )
  2. 加密 key
  3. 加密 key
  4. 加密 key
  5. 加密 key
  6. ENCRYPT_KEY
  7. 加密 key

Spring 使用 org.springframework.boot.bind.RelaxedPropertyResolver 解析上述 key 来获取 key ,但该类已在 spring-boot-2 中被弃用并删除>.

来自 org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration 类的 spring-cloud-context-1.x.jar 中的代码片段

Environment environment = context.getEnvironment();
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment);
hasProperty(propertyResolver, environment, "encrypt.key");

private boolean hasProperty(RelaxedPropertyResolver propertyResolver, Environment environment, String key) {
String value = propertyResolver.getProperty(key);
if (value == null) {
return false;
}
return StringUtils.hasText(environment.resolvePlaceholders(value));
}

Spring-boot-2

只有 encrypt.key 是传递 key 的有效 VM 参数。

来自 org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration 类的 spring-cloud-context-2.x.jar 中的代码片段

Environment environment = context.getEnvironment();
hasProperty(environment, "encrypt.key");

private boolean hasProperty(Environment environment, String key) {
String value = environment.getProperty(key);
if (value == null) {
return false;
}
return StringUtils.hasText(environment.resolvePlaceholders(value));
}

关于java - 将 Spring Boot 1.x 升级到 2.x(如果使用 {cipher} 文本,则更新 ENCRYPT KEY VM 参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58541498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com