将 J2SE 端点绑定到 HTTPS [英] Bind J2SE Endpoint to HTTPS

查看:48
本文介绍了将 J2SE 端点绑定到 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是创建服务器的代码:

 私有 HttpContext getHttpContext() {HttpsServer 服务器 = null;尝试 {server = HttpsServer.create(new InetSocketAddress("localhost", 443), 5);server.setHttpsConfigurator(new HttpsConfigurator(SecureChatSslContextFactory.getServerContext()){公共无效配置(HttpsParameters params){SSLContext 上下文 = getSSLContext();//获取默认参数SSLParameters sslparams = context.getDefaultSSLParameters();params.setSSLParameters(sslparams);}});server.start();return server.createContext("/customerservice/customer");} catch (IOException e) {e.printStackTrace();}返回空;}端点 e = Endpoint.create(HTTPBinding.HTTP_BINDING, new RestSourcePayloadProvider());e.publish(getHttpContext());

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

如果我在链接文本上运行测试客户端或浏览器,我会得到相同的答案:<块引用>

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

解决方案

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

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

代替

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

看起来接口Endpoint 是由Apache 的CFX 中的EndpointImpl 类实现的.在调用 publish(address) 时,EndpointImpl 中调用的方法会创建一个 ServerImpl 作为服务服务器.如果我调用 publish(server context),什么也不会发生,因为 EndpointImpl 也不会覆盖该方法.

I created a basic REST service that works when it is invoked over a http connection. But when I try to add SSL on top I have some problems.

This is the code for the server creation:

 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());

The class RestSourcePayloadProvider has the public method invoke, but it never gets called. I suppose that the reason for this behaviour is that the binding for the endpoint is HTTPBinding.HTTP_BINDING, and not HTTPS. But I didn't manage to find a way to bind to https.

If I run the test client or the browser on link text I get the same answer:

500 Internal Server Error No handler for context

解决方案

I found the answer to my problem. I was not creating a handler for the incoming requests. I was supposed to use

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

instead of

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

It looks like the interface Endpoint is implemented by the EndpointImplclass in Apache's CFX. When calling publish(address), the method called in EndpointImpl creates a ServerImpl that will be the service server. If I call publish(server context), nothing happens because EndpointImpl doesn't override that method also.

这篇关于将 J2SE 端点绑定到 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆