1

I have two jks files under src/main/resources in my Spring Boot rest app

  1. Test1.jks to make outbound call another rest api over https.
  2. Test2.jks to enable https in this rest api.

I am setting path to resource file test1.jks using relative like below in Spring Boot entry point which has been working fine. These jks files is used to call an https service.

public class TestApp {

    static
    {
        System.setProperty("javax.net.ssl.trustStore", "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
        System.setProperty("javax.net.ssl.trustStorePassword", password);
        System.setProperty("javax.net.ssl.keyStore",  "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
        System.setProperty("javax.net.ssl.keyStorePassword", password);
    }

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

application.properties

server.port=8443
server.ssl.key-store=test2.jks
server.ssl.key-store-password=test
server.ssl.trust-store=classpath:test2.jks
server.ssl.trust-store-password=test
server.ssl.key-password=test

When I run using mvn spring-boot:run everything works fine, however when I run it as executable jar java -jar target/xxx-xxx-service-0.1.1-SNAPSHOT.jar I am getting the following error.

Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Could not find key store classpath:test2.jks
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:292)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSsl(TomcatEmbeddedServletContainerFactory.java:271)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.customizeConnector(TomcatEmbeddedServletContainerFactory.java:248)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:147)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
        ... 13 more
Caused by: java.io.FileNotFoundException: class path resource [test2.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/xxx/xxx/Himalay/xxx/xxx-xxx-service/target/xxx-xxx-service-0.1.17-SNAPSHOT.jar!/test2.jks
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:212)
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:175)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:288)
        ... 18 more

After commenting application.properties I found it cannot read even test1.jks when run using the executable jar command but runs fine with mvn spring-boot:run, how can I make it run using executable jar command?

Note: If I use absolute path to the jks files, it works fine with both the commands.

Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • 3
    this might help: http://stackoverflow.com/questions/344748/how-to-use-a-file-in-a-jar-as-javax-net-ssl-keystore – wemu Jun 09 '16 at 01:19
  • As @wemu comment point out, you can not use getFile() on resource which is embedded in Jar. Use inputstream – Sangram Jadhav Jun 09 '16 at 03:36
  • Isn't this the same issue as with your other question? https://stackoverflow.com/questions/37713335/resource-filenotfoundexception-when-using-executable-jar-command-with-spring-boo – g00glen00b Jun 09 '16 at 06:28

1 Answers1

-1

Try this : String filePath= Thread.currentThread().getContextClassLoader().getResource("your-trust-store.jks").getFile(); System.setProperty("javax.net.ssl.keyStore", filepath);

safiuz
  • 9
  • 7