简体   繁体   English

如何设置基本的Jersey / Grizzly 2.21 SSL启动配置

[英]How to setup basic Jersey/Grizzly 2.21 SSL startup configuration

I'm trying to get a very basic Grizzly server up and running to allow for one-way SSL (HTTPS) connections to access jax-rs REST services. 我正在尝试启动并运行一个非常基本的Grizzly服务器,以允许单向SSL(HTTPS)连接访问jax-rs REST服务。 Eventually I want two-way SSL security. 最终,我需要双向SSL安全性。

I've gone through many of the examples and I just can't get anything to work. 我已经看过很多例子,但我什么都做不了。 I keep running into a SSL Handshake error. 我一直遇到SSL握手错误。 Clearly I must be doing something stupid. 显然我一定在做一些愚蠢的事情。 Any help is appreciated. 任何帮助表示赞赏。

Here is my code to start my embedded Grizzly server using the Jersey wrapper classes: 这是使用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;
}

I also tried replaced SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false) with null to see if that would help. 我还尝试将SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false)替换为null,以查看是否有帮助。 Nope. 不。

I always get the following error: 我总是收到以下错误:

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

To add on top of JMS comment, his answer solve my problem too . 除了JMS注释之外,他的回答也解决了我的问题。 Here is the command i used to generate the RSA certificate . 这是我用来生成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

I have seen this type of handshake issue come up in other posts while trying to run this issue to ground. 我曾在尝试将这一问题付诸实践时在其他帖子中看到过这种握手问题。 In all of these handshake posts, the server key algorithm was never discussed - I wish it had been. 在所有这些握手文章中,从未讨论过服务器密钥算法-我希望是这样。 It would have saved me a couple of hours. 这样可以节省我几个小时。 The issue that caused the above errors stemmed from assuming that the keystores that were created as part of the Jersey sample project would work. 导致上述错误的问题源于假定作为Jersey示例项目的一部分创建的密钥库可以工作的原因。 The server key was the problem. 服务器密钥是问题所在。

The sample server certs are generated using the DSA algorithm. 样本服务器证书是使用DSA算法生成的。 Apparently this is an issue. 显然这是一个问题。

I recreated the server keys using a RSA algorithm and 2048 bit strength. 我使用RSA算法和2048位强度重新创建了服务器密钥。 I restarted the server and the everything started to work like one would expect. 我重新启动服务器,一切开始按预期运行。

The error was that I assumed that the "sample" keys would work. 错误是我认为“示例”键可以工作。 Oops. 哎呀。

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

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