简体   繁体   English

Spark-java如何用jar里面的路径导入keystore文件

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

I have an Spark-java application that has a couple of endpoints. 我有一个Spark-java应用程序,它有几个端点。 Now i created a keystore file with a SSL certificate for my domain. 现在我为我的域创建了一个带有SSL证书的密钥库文件。

This is how i added it in spark: 这就是我在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. 如果我从intteliJ内部运行我的应用程序它能够运行,但是一旦我将我的应用程序打包到jar文件中,它就找不到密钥库文件。 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. 现在spark只允许你给出路径,而不是输入流。

Is there a way to still access a keystore file from within a jar, just by specifying an path? 有没有办法仍然从jar中访问密钥库文件,只需指定路径? 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. 在jar文件中打包应用程序时,无法找到src路径,因为它不再存在。
In Intelij IDEA / Project Structure windows / Modules section you can find Resource directory. Intelij IDEA / Project Structure窗口/ 模块部分中,您可以找到资源目录。 Every file/folder you put in Resource directory, would be copied in Jar file. 您放入资源目录的每个文件/文件夹都将被复制到Jar文件中。

But hard coding keyStorePath, keyStoreKey in your code is not secure. 但硬编码keyStorePath,代码中的keyStoreKey并不安全。 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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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