簡體   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