【问题标题】:How to setup basic Jersey/Grizzly 2.21 SSL startup configuration如何设置基本 Jersey/Grizzly 2.21 SSL 启动配置
【发布时间】:2015-11-20 16:26:45
【问题描述】:

我正在尝试启动并运行一个非常基本的 Grizzly 服务器,以允许单向 SSL (HTTPS) 连接访问 jax-rs REST 服务。最终我想要双向 SSL 安全性。

我已经浏览了许多示例,但我无法得到任何工作。我一直遇到 SSL 握手错误。显然我一定是在做一些愚蠢的事情。任何帮助表示赞赏。

这是我使用 Jersey 包装类启动嵌入式 Grizzly 服务器的代码:

public static HttpServer startHttpsServer(URI listenerURI) throws IOException  {
  ResourceConfig resourceConfig = new ResourceConfig().packages("ws.argo.experiment.ssl");

  // First I tried this configuration using the certs from the Jersey sample code
  // Grizzly ssl configuration
  SSLContextConfigurator sslContext = new SSLContextConfigurator();

  // set up security context
  sslContext.setKeyStoreFile("./src/main/resources/keystore_server"); // contains server keypair
  sslContext.setKeyStorePass("asdfgh");
  sslContext.setTrustStoreFile("./src/main/resources/truststore_server"); // contains client certificate
  sslContext.setTrustStorePass("asdfgh");

  // Then I tried just using a default config - didn't work either
  //    sslContext = SSLContextConfigurator.DEFAULT_CONFIG;


  if (!sslContext.validateConfiguration(true)) {
    LOGGER.severe("Context is not valid");

  }

  LOGGER.finer("Starting Jersey-Grizzly2 JAX-RS secure server...");
  HttpServer httpServer; //=   GrizzlyHttpServerFactory.createHttpServer(listenerURI, resourceConfig, false);


  httpServer= GrizzlyHttpServerFactory.createHttpServer(
      listenerURI,
      resourceConfig,
      true,
      new   SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false)
      );



  httpServer.getServerConfiguration().setName("Test HTTPS Server");
  httpServer.start();
  LOGGER.info("Started Jersey-Grizzly2 JAX-RS secure server.");

  return httpServer;
}

我还尝试将 SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false) 替换为 null 以查看是否有帮助。没有。

我总是收到以下错误:

grizzly-nio-kernel(3) SelectorRunner, fatal error: 40: no cipher suites in common
javax.net.ssl.SSLHandshakeException: no cipher suites in common
%% Invalidated:  [Session-2, SSL_NULL_WITH_NULL_NULL]
grizzly-nio-kernel(3) SelectorRunner, SEND TLSv1.2 ALERT:  fatal, description = handshake_failure
grizzly-nio-kernel(3) SelectorRunner, WRITE: TLSv1.2 Alert, length = 2
grizzly-nio-kernel(3) SelectorRunner, fatal: engine already closed.  Rethrowing javax.net.ssl.SSLHandshakeException: no cipher suites in common

【问题讨论】:

  • 您只需要服务器信任库中的客户端证书即可进行双向(双向)ssl。如果服务器证书是自签名的,您需要做的是将服务器证书添加到客户端信任库
  • 看看this example
  • @peetskillet - 这是我开始的例子。唯一的区别是我没有使用 jersey-container-grizzly2-servlet 设置。只需使用常规 jersey-container-grizzly2-http 版本作为依赖项。我认为这不会有什么不同。
  • 您使用的是 Jersey 客户端还是其他客户端?您是否按照说明创建商店和导入?该示例适用于我(也可以根据说明创建新商店)。您可以看到在测试用例中设置的 Jersey 客户端。我不认为从 servlet 更改为 http 配置应该有所不同,尽管我没有测试
  • 现在我只是在使用网络浏览器(Safari、Firefox、Chrome)来尝试访问资源。他们没有运气。它应该设置为允许使用浏览器进行简单的单向 SSL。我没有使用特殊的客户端。

标签: java rest ssl grizzly


【解决方案1】:

要添加 JMS 评论,他的回答也解决了我的问题。 这是我用来生成 RSA 证书的命令。

keytool -genkey -keystore ./keystore_client -alias clientKey -keyalg RSA -keypass changeit -storepass changeit -dname "CN=Client, OU=Jersey, O=changeit, L=KL, ST=SEL, C=MY"
keytool -export -alias clientKey -storepass changeit -keystore ./keystore_client -file ./client.cert
keytool -import -alias clientCert -file ./client.cert -storepass changeit -keystore ./truststore_server


keytool -genkey -keystore ./keystore_server -alias serverKey -keyalg RSA -keyalg RSA -keypass changeit -storepass changeit -dname "CN=changeit, OU=Jersey, O=changeit, L=KL, ST=SEL, C=MY"
keytool -export -alias serverKey -storepass changeit -keystore ./keystore_server -file ./server.cert
keytool -import -alias serverCert -file ./server.cert -storepass changeit -keystore ./truststore_client

【讨论】:

    【解决方案2】:

    在尝试解决此问题时,我在其他帖子中看到过此类握手问题。在所有这些握手帖子中,从未讨论过服务器密钥算法——我希望是这样。它会为我节省几个小时。导致上述错误的问题源于假设作为 Jersey 示例项目的一部分创建的密钥库可以工作。服务器密钥是问题所在。

    示例服务器证书是使用 DSA 算法生成的。显然这是个问题。

    我使用 RSA 算法和 2048 位强度重新创建了服务器密钥。我重新启动了服务器,一切都开始像预期的那样工作。

    错误是我认为“示例”键可以工作。哎呀。

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2014-06-12
      • 2014-02-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多