在项目中的何处包含信任库文件 [英] Where to include truststore files in project
问题描述
我正在尝试了解 SSL 并成功制作了一个通过 SSL 连接进行通信的客户端/服务器程序.我已经生成了我自己的信任库和密钥库文件,我希望我的客户端和服务器程序使用它们.到目前为止,我可以使用以下命令通过命令行运行这两个程序:
I am trying to learn about SSL and have successfully made a client/server program that communicates over an SSL connection. I have generated my own truststore and keystore files that I would like my client and server programs to use. So far I am able to run both programs with through the command line using the following commands:
java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 ServerMain.java
和
java -Djavax.net.ssl.trustStore=mySrvKeystore -Djavax.net.ssl.trustStorePassword=123456 Client.java
但是,我想知道是否有可能以某种方式将文件实现到我的项目中,以便将来将其构建到 .jar 文件中时,我可以将这些文件与它捆绑在一起.以前,我通过将图像存储在我的项目文件夹中的 res 文件夹中并使用/res/image.jpg"作为路径对图像进行了类似的操作,并且效果很好.我已经通过使用 System.setProperty("javax.net.ssl.trustStore", "/res/mySrvkeystore.jks")
尝试了相同的方法,但它一直返回未找到的文件.任何帮助都会很棒!谢谢!
However, I would like to know if there is a possible way to implement the files into my project somehow so that in the future when I build it into a .jar file, I can bundle the files with it. Previously, I had done something similar with images by storing them in a res folder which is in my project folder and used "/res/image.jpg" as a path and it worked fine. I have tried the same method by using the System.setProperty("javax.net.ssl.trustStore", "/res/mySrvkeystore.jks")
but it keeps returning file not found. Any help would be great! Thanks!
推荐答案
- 将密钥库作为资源包含在您的 JAR 文件中,就像您为图像所做的那样.
- 通过
getResourceAsStream() 访问它.
- 使用该
InputStream
加载KeyStore.
- 使用
KeyStore
初始化TrustManager.
- 使用
TrustManager
初始化一个SSLContext.
- 使用
SSLContext
创建您的SSLSocketFactory.
- 使用
SSLSocketFactory
创建您的SSLSocket.
- Include the keystore in your JAR file as a resource, as you did for your images.
- Access it via
getResourceAsStream().
- Use that
InputStream
to load aKeyStore.
- Use the
KeyStore
to initialize aTrustManager.
- Use the
TrustManager
to initialize anSSLContext.
- Use the
SSLContext
to create yourSSLSocketFactory.
- Use the
SSLSocketFactory
to create yourSSLSocket.
这篇关于在项目中的何处包含信任库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!