简体   繁体   中英

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

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. Why please help.

loadTrustMaterial method loads truststore file using a URL, so you need to provide fully qualified URL.

For that reason, provide truststore file as file:///config/truststore.p12 instead of /config/truststore.p12 .

So, your JVM argument should be something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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