Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Java app not finding the keystore file

I am trying to set up a simple Spring application to use SSL and host it on Digital Ocean. Why is my app not finding the keystore file?

The droplet I've set up is based on Ubuntu 18.04. I used Letsencrypt to get a certificate and this guide to generate a PKCS file. I've set up my application.properties file to look in the jar file's current directory like so:

security.require-ssl:true
server.ssl.key-store:keystore.p12
server.ssl.key-store-password:<password>
server.ssl.key-store-type:PKCS12
server.ssl.key-alias:<alias>

I would expect this to run and start a web server on the configured port. However, what I get in the stack trace is this:

Caused by: java.io.FileNotFoundException: /root/software/gimmememe/target/keystore.p12  (No such file or directory)

Weirdly enough when I run the same jar with the same keystore.p12 file on my own Windows machine it runs fine:

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9123 (https) with context path ''
meme.Application                         : Started Application in 4.985 seconds (JVM running for 5.464)

I don't think it's a permissions issue on the Ubuntu machine as I tried setting the permissions on the keystore file like so:

-rw-r--r-- 1 root root     4274 Mar 26 18:44 keystore.p12

I am running my jar file with the following command (tried with sudo infront as well):

java -jar gimme-meme-1.0.0.war
like image 366
MZokov Avatar asked Mar 26 '19 19:03

MZokov


2 Answers

Spring loads the file from the classpath, which allows, so you should prefix the path with that classpath:, e.g.

server.ssl.key-store : classpath:keystore.p12

Or if you use the = symbol as a key/value delimiter:

server.ssl.key-store = classpath:keystore.p12

Bear in mind that the value is only trimmed on the left side, so you can not have any trailing whitespace after the value.

like image 166
isapir Avatar answered Sep 23 '22 08:09

isapir


I had exactly the same issues and could resolve it.
I stored the keystore file in src/main/resources/keystore.p12, but in the jar file it was under classes/ directly.
My solution was:

server.ssl.key-store=classpath:keystore.p12   
like image 40
frank Avatar answered Sep 22 '22 08:09

frank