设置 Grizzly 的密钥库以使用 jar 中的 jks

发布于 2024-08-18 17:43:44 字数 383 浏览 8 评论 0原文

我正在尝试使用 com.sun.grizzly.SSLConfig.setKeyStoreFile() 为 Grizzly 设置 SSL。它只接受 String 作为输入(而不是 InputStream 或 File)。我想使用 JAR 文件中的 jks 文件。如果我传递一个 jar 路径的字符串(例如 C:\dir\my.jar!\resources\my.jks),它将失败。除了从 JAR 中解压缩文件之外,我如何将 JKS 用于 grizzly.

I'm trying to use com.sun.grizzly.SSLConfig.setKeyStoreFile() to set SSL for Grizzly. It only takes a String as input (not InputStream or File). I want to use a jks file that is within a JAR file. If I pass a string for a jar path (eg C:\dir\my.jar!\resources\my.jks), it fails. Other than just unzipping the file from the JAR, how can I use that JKS for grizzly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-08-25 17:43:44

除了文件名之外,您似乎无法传递任何内容。如果您查看 source 并查看 validateConfiguration()createSSLContext() 方法,您会发现它正在传递 keyStoreFile 变量直接放入 FileInputStream 构造函数中。

短期内,您可能会被迫解压缩并使用直接文件名。或者您可以重写上面列出的两个方法来正确验证和初始化 SSLContext。从长远来看,我会提交补丁。

It doesn't appear you can pass in anything other than a filename. If you view the source and look at the validateConfiguration() and createSSLContext() methods, you'll see that it is passing the keyStoreFile variable directly into the FileInputStream constructor.

Short term, you're probably stuck with unzipping and using the direct file name. Or you could override the two methods listed above to properly validate and initialize the SSLContext. Long term, I'd submit a patch.

幼儿园老大 2024-08-25 17:43:44

@Kevin的想法成功了!使用 grizzly-servlet-webserver 1.9.8,这是我的代码:


SSLConfig ssl = new SSLConfig(){
 @Override
 public SSLContext createSSLContext() { 
  try{
   //Load the keystore.
   KeyStore keyStore=KeyStore.getInstance(KeyStore.getDefaultType());
   InputStream keyStream=ClassLoader.getSystemResourceAsStream("my.jks");
   //InputStream keyStream=new java.net.URL("jar:file:/C:/dir/my.jar!/my.jks").openStream();
   keyStore.load(keyStream,"mypassword");
   keyStream.close();

   //Create the factory from the keystore.
   String kmfAlgorithm=System.getProperty("ssl.KeyManagerFactory.algorithm",KeyManagerFactory.getDefaultAlgorithm());
   KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(kmfAlgorithm);
   keyManagerFactory.init(keyStore,"mypassword");

   //Create the SSLContext
   SSLContext sslContext=SSLContext.getInstance("TLS");
   sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
   return sslContext;
  }

  //Wrap all Exceptions in a RuntimeException.
  catch(Exception e){
   throw new RuntimeException(e);
  }
 }
};

我采取了一些快捷方式(不记录异常,使用多个字符串常量等),但您可以明白这个想法。

@Kevin's idea worked! Using grizzly-servlet-webserver 1.9.8, here's my code:


SSLConfig ssl = new SSLConfig(){
 @Override
 public SSLContext createSSLContext() { 
  try{
   //Load the keystore.
   KeyStore keyStore=KeyStore.getInstance(KeyStore.getDefaultType());
   InputStream keyStream=ClassLoader.getSystemResourceAsStream("my.jks");
   //InputStream keyStream=new java.net.URL("jar:file:/C:/dir/my.jar!/my.jks").openStream();
   keyStore.load(keyStream,"mypassword");
   keyStream.close();

   //Create the factory from the keystore.
   String kmfAlgorithm=System.getProperty("ssl.KeyManagerFactory.algorithm",KeyManagerFactory.getDefaultAlgorithm());
   KeyManagerFactory keyManagerFactory=KeyManagerFactory.getInstance(kmfAlgorithm);
   keyManagerFactory.init(keyStore,"mypassword");

   //Create the SSLContext
   SSLContext sslContext=SSLContext.getInstance("TLS");
   sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
   return sslContext;
  }

  //Wrap all Exceptions in a RuntimeException.
  catch(Exception e){
   throw new RuntimeException(e);
  }
 }
};

I took a few shortcuts (not logging Exceptions, using several string constants, etc), but you can get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文