Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot embedded tomcat not loading SSL keystore file from classpath

I am using Spring boot 1.2.7 for my application and as a requirement I have to load the SSL cert from classpath. So, I added my PKCS12 file in my classpath and used following code (in AppInitializer class to load it:

final String sslKeystoreFilepath = this.getClass().getClassLoader().getResource(sslKeystoreFilename).getFile();

I am noticing two things here:

  1. The string contains file: in it. If I run the same code through a plain standalone program with main method, I couldn't see anything like file:.

  2. When I run the Spring boot app (using generated fat jar), it throws exception saying:

SEVERE: Failed to load keystore type PKCS12 with path /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 due to /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 (No such file or directory) java.io.FileNotFoundException: /Users/my_user/Projects/my_app/build/libs/myapp-1.0-SNAPSHOT-dev.jar!/dev_keystore.p12 (No such file or directory)

What am I doing wrong?

like image 508
Niranjan Avatar asked Oct 30 '15 05:10

Niranjan


People also ask

How do I specify local file path to my SSL keystore file in spring application properties?

You need to use file:// . FileNotFoundException: C:\Windows\Temp\tomcat. 8707914234839952642.1111\file:\c:\path\to\file\keystore.

How does spring boot embedded Tomcat work?

Spring Boot has a complete Tomcat inside. It builds a so-called fat-jar with everything needed inside. You don't need Tomcat installed in your system. BTW: Spring Boot also supports other application servers like Jetty.


Video Answer


2 Answers

I think you can add this configuration in your .properties file as belows, rather than getting the string,

server.ssl.key-store: classpath:dev_keystore.p12
server.ssl.key-store-password: mypassword
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat

If you need to do what you are doing now, let me know, I will give that answer

like image 80
diyoda_ Avatar answered Sep 24 '22 00:09

diyoda_


In this answer seems that Tomcat can't read the keystore conatined in a jar, but I'm not sure of that because I use Spring Boot 1.2.5 and everything works like a charme with the keystore in the classpath. However I've exactly the same error of yours when I try to upgrade to Spring Boot 1.2.7 and above, so I concluded that depends on Spring Boot. At this time, I downgraded to 1.2.5 version. :(

like image 35
lincetto Avatar answered Sep 27 '22 00:09

lincetto