简体   繁体   中英

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

I have successfully created an embedded HttpServer using the example at https://hc.apache.org.httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/HttpFileServer.java , which handles http: traffic just fine. Now I would like to extend this to support TLS.

I copied some likely looking code from here: https://hc.apache.org/httpcomponents-core-ga/tutorial/html/blocking-io.html#d5e455

But some parts of it are showing up as deprecated, which makes me wonder whether this example is out of date and whether the tree I'm at is the one where I should be barking. Even if that weren't the case, I am having trouble finding the relationship between HttpServer and DefaultBHttpClientConnection (mentioned in the example). I suspect that I should be using DefaultBHttpServerConnection but I am so far unable to find that either.

Is there a newer example of this anywhere?

Michael D. Spence Mockingbird Data Systems, Inc.

I am not sure understand the problem you are having. All you need is to provide a correctly initialized SSLContext instance to ServerBoostrap . HttpCore ships with SSLContextBuilder specifically designed to simplify the process of SSLContext initialization.

The example included in HttpCore distribution pretty much shows every step required to set up SSL/TLS transport layer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM