Jetty http2客户端忽略自签名证书

时间:2016-10-04 22:55:52

标签: java ssl jetty http2

我在https://ec2-52-57-54-142.eu-central-1.compute.amazonaws.com/有一个带有自签名证书的http2服务器。我有一个码头http2客户端,它只是简单地发布它。出于某种原因,我不会忽视自签名证书。这是相关代码的片段

SslContextFactory factory = new SslContextFactory(true);
factory.setTrustAll(true);
factory.setValidateCerts(false);
factory.setValidatePeerCerts(false);
factory.setEndpointIdentificationAlgorithm(null);

SSLContext sslContext = factory.getSslContext();
if(null == sslContext) {
    sslContext = SSLContext.getInstance("TLS");
}
TrustManager[] verifiers = new TrustManager[] {...// some dummy trust manager that always passes};
sslContext.init(null, verifiers, null);
factory.setSslContext(sslContext);

HttpClientTransportOverHTTP2 httpClientTransportOverHTTP2
            = new HttpClientTransportOverHTTP2(new HTTP2Client());
HttpClient httpClient = new HttpClient(httpClientTransportOverHTTP2, factory);


Request request = httpClient.POST(destination);
ContentProvider contentProvider = new InputStreamContentProvider(new StringInputStream(payload));
request.content(contentProvider);
ContentResponse response = request.send();

我得到了这些堆栈跟踪

Caused by: java.util.concurrent.ExecutionException: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
    at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
    at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:652)
    at my code
    ... 34 more
Caused by: java.nio.channels.ClosedChannelException
    at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:498)
    at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:409)
    at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54)
    at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:322)
    at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:140)
    at org.eclipse.jetty.http2.HTTP2Flusher.process(HTTP2Flusher.java:243)
    at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241)
    at org.eclipse.jetty.util.IteratingCallback.succeeded(IteratingCallback.java:365)
    at org.eclipse.jetty.http2.HTTP2Flusher.succeeded(HTTP2Flusher.java:258)
    at org.eclipse.jetty.io.WriteFlusher$PendingState.complete(WriteFlusher.java:269)
    at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:394)
    at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:101)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    ... 1 more

当我使用调试器进行操作时,我会在其中一个连接对象中看到有关NOT_HANDSHAKE的信息。

我真的不在乎验证任何事情。我只想通过HTTP2 / TLS连接。我搜索了许多不同的术语,但它们最终都或多或少地相同(setTrustAll,自定义TrustManager等)

有任何帮助吗?谢谢!

P.S。 Jetty版本9.3.12

2 个答案:

答案 0 :(得分:2)

您的客户端代码是正确的,尽管是多余的。 这样就足够了:

    SslContextFactory sslContextFactory = new SslContextFactory(true);

    HTTP2Client http2Client = new HTTP2Client();
    HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
    httpClient.start();

    ContentResponse response = httpClient.GET("https://ec2-52-57-54-142.eu-central-1.compute.amazonaws.com/");

如果在Jetty HTTP / 2客户端上启用DEBUG日志记录,您将看到客户端收到:

2016-10-05 09:20:33.102:DBUG:oejhp.Parser:qtp1897115967-15: Parsed GO_AWAY frame header from java.nio.HeapByteBuffer[pos=9 lim=35 cap=16384]
2016-10-05 09:20:33.103:DBUG:oejh.HTTP2Session:qtp1897115967-15: Received GoAwayFrame@3bc447d3,0/INADEQUATE_SECURITY_ERROR/Unknown error code

所以问题是服务器认为安全性不足(GOAWAY帧到达时出现错误代码INADEQUATE_SECURITY_ERROR)。

此时,问题出现在服务器上。您必须弄清楚服务器认为安全性不足的原因。可能只是服务器上的配置问题。

答案 1 :(得分:0)

尝试

transport.setUseALPN(false);