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

问题描述 投票:0回答:1

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

[application.ymlapplication.properties

my.password='{cipher}68e78a954bfa0297ecc733`

以上在SpringBoot2中开始失败,并显示错误消息Cannot decrypt: 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)

java spring encryption encryption-symmetric spring-boot-2
1个回答
0
投票

Spring-boot-1

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

  1. encrypt.key(默认密钥)
  2. encrypt_key
  3. encryptKey
  4. 加密密钥
  5. 加密密钥
  6. ENCRYPT_KEY
  7. ENCRYPTKEY

春季使用org.springframework.boot.bind.RelaxedPropertyResolver解析上述密钥以获取秘密密钥,但该类已被弃用,并在spring-boot-2中删除。

类别spring-cloud-context-1.x.jarorg.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration中的代码段

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是有效的VM参数,用于传递密钥。

类别spring-cloud-context-2.x.jarorg.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration中的代码段

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));
        }
© www.soinside.com 2019 - 2024. All rights reserved.