如何通过SSL和基本身份验证保护Grizzly

时间:2017-03-14 11:51:15

标签: java ssl basic-authentication grizzly

我有以下代码,以通过SSL保护Grizzly服务器并另外使用基本身份验证:

  public static HttpServer getGrizzlyServer(String bindUri, String restClientUri, ResourceConfig rc) throws Exception
  {
    HttpServer result = new HttpServer();
    URL bindURL = null;
    try
    {
      bindURL = new URL(bindUri);
    }
    catch (MalformedURLException e)
    {
      throw e;
    }

    final NetworkListener listener = new NetworkListener("grizzly", bindURL.getHost(), bindURL.getPort());

    result.addListener(listener);
    // do we need SSL?
    listener.setSecure(true);
    SSLEngineConfigurator sslEngineConfigurator = createSSLConfig();
    listener.setSSLEngineConfig(sslEngineConfigurator);
    // Map the path to the processor.
    final ServerConfiguration config = result.getServerConfiguration();
    final HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class, rc);
    config.addHttpHandler(handler, "/");
    result.start();
    return result;
  }

  private static SSLEngineConfigurator createSSLConfig() throws Exception
  {
    final SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator();
    // override system properties
    File cacerts = new File(
        GrizzlyServerUtil.class.getResource("truststore.ts").getFile());
    if (cacerts.exists())
    {
      sslContextConfigurator.setTrustStoreFile(cacerts.getAbsolutePath());
      sslContextConfigurator.setTrustStorePass("pw");
    }

    // override system properties
    File keystore = new File(
        GrizzlyServerUtil.class.getResource("keystore.jks").getFile());
    if (keystore.exists())
    {
      sslContextConfigurator.setKeyStoreFile(keystore.getAbsolutePath());
      sslContextConfigurator.setKeyStorePass("pw");
    }

    //
    boolean clientMode = false;
    // force client Authentication ...
    boolean needClientAuth = true;
    boolean wantClientAuth = false;
    SSLEngineConfigurator result = new SSLEngineConfigurator(sslContextConfigurator.createSSLContext(), clientMode, needClientAuth,
        wantClientAuth);
    return result;
  }

现在我的问题是,如果我使用以下信息启动服务器:

final ResourceConfig rc = new ResourceConfig().packages("ask.dolserveragent");
rc.property("restClientUri", baseRestUri);
rc.register(AuthenticationFilter.class);

try
{
  HttpServer httpServer = GrizzlyServerUtil.getGrizzlyServer(bindUri, baseRestUri, rc);
  return httpServer;
}
catch (Exception e)
{
  e.printStackTrace();
  return null;
}

我无法使用https://LOCAL_IP_ADDRESS:1337/application.wadl访问服务器。 我尝试使用和不使用基本身份验证,但网站根本无法访问。 关于SSL和基本身份验证有什么问题,或者我错了什么?

0 个答案:

没有答案