【问题标题】:Bind J2SE Endpoint to HTTPS将 J2SE 端点绑定到 HTTPS
【发布时间】:2011-06-11 17:31:39
【问题描述】:

我创建了一个基本的 REST 服务,该服务在通过 http 连接调用时工作。但是当我尝试在顶部添加 SSL 时,我遇到了一些问题。

这是创建服务器的代码:

 private HttpContext getHttpContext() {
  HttpsServer server = null;
  try {
   server = HttpsServer.create(new InetSocketAddress("localhost", 443), 5);

   server.setHttpsConfigurator(new HttpsConfigurator(SecureChatSslContextFactory.getServerContext()) {

    public void configure(HttpsParameters params) {
     SSLContext context = getSSLContext();

     // get the default parameters
     SSLParameters sslparams = context.getDefaultSSLParameters();
     params.setSSLParameters(sslparams);
    }
   });

   server.start();

   return server.createContext("/customerservice/customer");
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 } 
    Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING, new RestSourcePayloadProvider()); 
    e.publish(getHttpContext());

RestSourcePayloadProvider 类有公共方法调用,但它永远不会被调用。我想这种行为的原因是端点的绑定是HTTPBinding.HTTP_BINDING,而不是HTTPS。但是我没有找到绑定到 https 的方法。

如果我在link text 上运行测试客户端或浏览器,我会得到相同的答案:

500 内部服务器错误 没有上下文处理程序

【问题讨论】:

    标签: java web-services restful-authentication


    【解决方案1】:

    我找到了问题的答案。我没有为传入的请求创建处理程序。 我应该使用

    return server.createContext("/customerservice/customer", new HttpHandler(...));
    

    而不是

    return server.createContext("/customerservice/customer");
    

    看起来接口Endpoint是由Apache的CFX中的EndpointImpl类实现的。当调用publish(address) 时,EndpointImpl 中调用的方法会创建一个ServerImpl,它将作为服务服务器。如果我调用publish(server context),则不会发生任何事情,因为EndpointImpl 也不会覆盖该方法。

    【讨论】:

      【解决方案2】:

      对于那些想要使用 CFX 的 EndpointImpl 和 SSL 的人来说,试试这个: link text

      它使用 Jetty Server 包来创建工作服务器并将其发布到端点。

      【讨论】:

        猜你喜欢
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2013-01-14
        • 2011-06-15
        相关资源
        最近更新 更多