【问题标题】:Spring cloud loading keystore from classpath vs fileSpring Cloud从类路径与文件加载密钥库
【发布时间】:2020-10-13 13:31:49
【问题描述】:

我有一个 spring 云配置服务器,它使用密钥库来解密来自 git 服务器的值。如果我使用文件路径引用密钥库,它会按预期工作并解密 {cipher} 值。但是,如果我尝试从类路径加载密钥库,它将停止处理此错误: CipherEnvironmentEncryptor.decrypt - 无法解密密钥:用户名(类 java.lang.IllegalStateException:无法从存储区加载密钥:类路径资源 [mykey.p12])

我在类而不是 yaml 上设置加密属性,因为我需要从外部保管库系统中查找 dev/prod 密钥库的不同密码。
我还可以在构建后在 target/classes 下看到 p.12 文件,因此在构建期间它不会被过滤掉。不知道我错过了什么。

    SpringApplication sa = new SpringApplication(Myclass.class);
    Properties springProperties = new Properties();
    springProperties.setProperty("encrypt.enabled", "true");
    springProperties.setProperty("encrypt.key-store.location", "file:///Users/user/IdeaProjects/project/src/main/resources/configuration/mykey.p12"); //working fine
    springProperties.setProperty("encrypt.key-store.location", "classpath:/configuration/mykey.p12");  //does not work
    springProperties.setProperty("encrypt.key-store.type", "PKCS12");
    springProperties.setProperty("encrypt.key-store.password", "password");
    springProperties.setProperty("encrypt.key-store.secret", "password");
    springProperties.setProperty("encrypt.key-store.alias", "vault");

    sa.setDefaultProperties(springProperties);
    sa.run(args);

使用

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.0.RELEASE</version>
<name>spring-cloud-config-server</name>

【问题讨论】:

    标签: spring encryption spring-cloud


    【解决方案1】:

    我的问题实际上与此有关:generated certificate stops working when moved to resources folder

    我对破坏最终 p12 文件的资源进行了 maven 过滤配置。现在我只是将需要过滤的文件移动到另一个资源目录并且它可以工作。

    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
         <resource>
             <directory>src/main/resources-filtered</directory>
              <filtering>true</filtering>
                <includes>
                  <include>*.yml</include>
                  <include>logback.xml</include>
                </includes>
          </resource>
      </resources>
    

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 2017-01-27
      • 2018-12-19
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 2014-10-13
      相关资源
      最近更新 更多