繁体   English   中英

如何在 Spring 引导应用程序中从 linux 文件系统(不是从类路径)加载信任库

[英]How to load trust store from linux file system (not from classpath) in Spring Boot application

我无法使用以下方法加载信任存储文件,

  @Value("${app.ssl.trust-store}")
  private Resource trustStore;    
@Bean("restTemplateForCustom")
      public RestTemplate restTemplateForCustom(final RestTemplateBuilder builder)
          throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
          CertificateException, IOException {
        final SSLContext sslContext =
            new SSLContextBuilder().loadTrustMaterial(trustStore.getFile(), trustStorePass).build();
        return new RestTemplateBuilder().build();
      }

即使我尝试了以下方法

@Value("${app.ssl.trust-store}")
  private Resource trustStore;
    @Bean("restTemplateForCustom")
      public RestTemplate restTemplateForCustom(final RestTemplateBuilder builder)
          throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException,
          CertificateException, IOException {
        final SSLContext sslContext =
            new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePass).build();
        return new RestTemplateBuilder().build();
      }

尝试使用以下行传递文件,

-Dapp.ssl.trust-store=/config/truststore.p12

例外:

java.io.FileNotFoundException: /tmp/tomcat-docbase.8080.1011071379153590118/config/truststore.p12 (No such file or directory)

预期:我有来自同一 jar 位置的配置文件夹,并尝试从文件系统中选择密钥库,但它来自 tmp。 为什么请帮忙。

loadTrustMaterial方法使用 URL 加载信任库文件,因此您需要提供完全合格的 URL。

因此,将信任库文件提供为file:///config/truststore.p12而不是/config/truststore.p12

因此,您的 JVM 参数应该类似于:

-Dapp.ssl.trust-store=file:///config/truststore.p12

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM