简体   繁体   中英

Spark-java how to import keystore file with path inside jar

I have an Spark-java application that has a couple of endpoints. Now i created a keystore file with a SSL certificate for my domain.

This is how i added it in spark:

secure("src/deploy/letsencrypt.jks", "mysecretpassword", null, null);

If i run my application from inside intteliJ it is able to run, but once i package my application in a jar file it can't find the keystore file. I have searched a lot on the web and the answer to the question is: use an inputstream. Now spark only lets you give an path, not an inputstream.

Is there a way to still access a keystore file from within a jar, just by specifying an path? Or is this not possible atm with spark?

When you package an application in a jar file, the src path could not be found because it doesn't exist anymore.
In Intelij IDEA / Project Structure windows / Modules section you can find Resource directory. Every file/folder you put in Resource directory, would be copied in Jar file.

But hard coding keyStorePath, keyStoreKey in your code is not secure. I suggest set these variable as parameters in run-time. something like this code:

String keyStorePath = System.getProperty("app.httpskey.path");
        String keyStoreKey = System.getProperty("app.httpskey.key");
        if (keyStoreKey != null && keyStorePath != null) {
            try {
                secure(keyStorePath, keyStoreKey, null, null);
                Logger.log("Web server will started in HTTPS mode.");
            } catch (Exception e) {
                Logger.log(e);
            }
        }

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