简体   繁体   English

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

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

i am unable to load the trust store file using below approach,我无法使用以下方法加载信任存储文件,

  @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();
      }

Even i tried with below approach即使我尝试了以下方法

@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();
      }

trying to pass the file using below line,尝试使用以下行传递文件,

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

Exception:例外:

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

Expected: i have config folder from same jar location and trying to pick the keystore from filesystem but its referring from tmp.预期:我有来自同一 jar 位置的配置文件夹,并尝试从文件系统中选择密钥库,但它来自 tmp。 Why please help.为什么请帮忙。

loadTrustMaterial method loads truststore file using a URL, so you need to provide fully qualified URL. loadTrustMaterial方法使用 URL 加载信任库文件,因此您需要提供完全合格的 URL。

For that reason, provide truststore file as file:///config/truststore.p12 instead of /config/truststore.p12 .因此,将信任库文件提供为file:///config/truststore.p12而不是/config/truststore.p12

So, your JVM argument should be something like:因此,您的 JVM 参数应该类似于:

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

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

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