【问题标题】:Example of using ssl with org.apache.http.impl.bootstrap.HttpServer from Apache HttpCore 4.4.3使用 ssl 与 Apache HttpCore 4.4.3 中的 org.apache.http.impl.bootstrap.HttpServer 的示例
【发布时间】:2015-09-16 20:30:06
【问题描述】:

我已经使用https://hc.apache.org.httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/HttpFileServer.java 的示例成功创建了一个嵌入式 HttpServer,它可以很好地处理 http: 流量。现在我想扩展它以支持 TLS。

我从这里复制了一些看起来很可能的代码:https://hc.apache.org/httpcomponents-core-ga/tutorial/html/blocking-io.html#d5e455

但它的某些部分显示为已弃用,这让我想知道这个示例是否已过时,以及我所在的树是否是我应该吠叫的树。即使不是这种情况,我也很难找到 HttpServer 和 DefaultBHttpClientConnection 之间的关系(在示例中提到)。我怀疑我应该使用 DefaultBHttpServerConnection 但到目前为止我也找不到。

这方面有没有更新的例子?

迈克尔·D·斯宾塞 知更鸟数据系统公司

【问题讨论】:

    标签: java ssl apache-httpcomponents


    【解决方案1】:

    我不确定是否理解您遇到的问题。您只需要为 ServerBoostrap 提供正确初始化的 SSLContext 实例。 HttpCore 附带SSLContextBuilder,专门用于简化SSLContext 初始化的过程。

    HttpCore 发行版中包含的示例几乎显示了设置 SSL/TLS 传输层所需的每个步骤。

    SSLContext sslcontext = null;
    if (port == 8443) {
        // Initialize SSL context
        URL url = HttpFileServer.class.getResource("/my.keystore");
        if (url == null) {
            System.out.println("Keystore not found");
            System.exit(1);
        }
        sslcontext = SSLContexts.custom()
                .loadKeyMaterial(url, "secret".toCharArray(), "secret".toCharArray())
                .build();
    }
    
    SocketConfig socketConfig = SocketConfig.custom()
            .setSoTimeout(15000)
            .setTcpNoDelay(true)
            .build();
    
    final HttpServer server = ServerBootstrap.bootstrap()
            .setListenerPort(port)
            .setServerInfo("Test/1.1")
            .setSocketConfig(socketConfig)
            .setSslContext(sslcontext)
            .setExceptionLogger(new StdErrorExceptionLogger())
            .registerHandler("*", new HttpFileHandler(docRoot))
            .create();
    
    server.start();
    

    【讨论】:

    • Re: sslcontext = SSLContexts.custom() SSLContexts 在 Netbeans 中显示为已弃用。但看来您的意思是改为 SSLContextBuilder 。这似乎可行,但现在我在运行时得到了可怕的“没有共同的密码套件”。
    • org.apache.http.ssl.SSLContexts 未被弃用hc.apache.org/httpcomponents-core-4.4.x/httpcore/xref/org/…。就您的 SSL 代码而言,这是一个完全不同的故事。自然,客户端和服务器端都需要具有兼容的设置才能使传输安全正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2012-10-31
    • 2011-06-22
    • 2020-02-11
    相关资源
    最近更新 更多