簡體   English   中英

從Apache HttpCore 4.4.3將ssl與org.apache.http.impl.bootstrap.HttpServer一起使用的示例

[英]Example of using ssl with org.apache.http.impl.bootstrap.HttpServer from Apache HttpCore 4.4.3

我已經使用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·斯潘斯·知更鳥數據系統公司

我不確定您是否遇到了問題。 您所需ServerBoostrapServerBoostrap提供一個正確初始化的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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM