如何使HttpClient 5的Conscrypt SSL Provider在Tomcat Web应用程序中工作

时间:2018-12-17 21:31:37

标签: java ssl tomcat http2 apache-httpclient-5.x

我正在使用Apache HttpClient 5Conscrypt来通过SSL同时执行HTTP 2.0请求,如下所示:

final SSLContext sslContext;
    try {
        sslContext = SSLContexts.custom()
                .setProvider(Conscrypt.newProvider())
                .build();
    } catch (Exception e) {
         // ... omitted for brevity
    }

    final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
            .setTlsStrategy(new ConscriptClientTlsStrategy(sslContext))
            .build();

    final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
            .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
            .setConnectionManager(cm)
            .build();

    try {
        httpclient.start();
        HttpHost host = new HttpHost("www.wikidata.org");
        final HttpClientContext clientContext = HttpClientContext.create();

        final SimpleHttpRequest request = SimpleHttpRequests.GET.create(host, "/w/api.php?action=wbsearchentities&search=Washington");
            request.addHeader("Accept-Charset", charset);
            FutureCallback<SimpleHttpResponse> callback = // ... omitted for brevity
            httpclient.execute(SimpleRequestProducer.create(request),
                    SimpleResponseConsumer.create(),
                    clientContext,
                    callback);
     }
     catch (Exception e) {
         // ... omitted for brevity
     }

在单元测试中运行代码成功。但是,如果作为在Tomcat v8中运行的Web应用程序的一部分运行,它将在调用Conscrypt.newProvider()上引发以下异常: message: java.lang.UnsatisfiedLinkError: Failed creating temp file (null)。看来是特权问题。有人可以指出在这种情况下我应该配置什么来解决问题吗?

1 个答案:

答案 0 :(得分:0)

您需要在类路径中设置 conscrypt-openjdk-uber-1.4.2.jar 而不是 conscrypt-openjdk-1.4.2.jar ,希望这可以解决您的问题问题,因为uber jar将拥有conscrypt所需的所有依赖项。

还使用 Http2AsyncClientBuilder 代替HttpAsyncClients进行http2多路复用。