在项目中的何处包含信任库文件 [英] Where to include truststore files in project

查看:37
本文介绍了在项目中的何处包含信任库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 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!

推荐答案

  1. 将密钥库作为资源包含在您的 JAR 文件中,就像您为图像所做的那样.
  2. 通过 getResourceAsStream() 访问它.
  3. 使用该 InputStream 加载 KeyStore.
  4. 使用 KeyStore 初始化 TrustManager.
  5. 使用 TrustManager 初始化一个 SSLContext.
  6. 使用 SSLContext 创建您的 SSLSocketFactory.
  7. 使用 SSLSocketFactory 创建您的 SSLSocket.
  1. Include the keystore in your JAR file as a resource, as you did for your images.
  2. Access it via getResourceAsStream().
  3. Use that InputStream to load a KeyStore.
  4. Use the KeyStore to initialize a TrustManager.
  5. Use the TrustManager to initialize an SSLContext.
  6. Use the SSLContext to create your SSLSocketFactory.
  7. Use the SSLSocketFactory to create your SSLSocket.

这篇关于在项目中的何处包含信任库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆