Ignore selbstsignierte Zertifikate in Apache HTTPClient 4.5

Ich versuche zukzeptieren Sie alle Zertifikate und / oder akzeptieren Sie selbstsignierte Zertifikat usingApache HTTPClient Version 4.5 (Tutorial LinkHie)

Ich habe Lösungen für dieses Problem aus einer Reihe von Beiträgen auf SO durchgearbeitet. Bisher hat keiner von ihnen gearbeitet.

Ich bekomme immer diesen Fehler:Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

Apache Docs:

Apache v4.5 Tutorial SSL / TLS-Anpassung Apache hat eine Anleitung für Version 3, aber nicht Version 4.

Related StackOverflow Questions - Hier sind einige Links zu den Lösungen, die ich ausprobiert habe:

Ignoring SSL-Zertifikat in Apache HttpClient 4.3 So ignorieren Sie SSL-Zertifikatfehler in Apache HttpClient 4.0Ignore SSL Certificate Errors with JavaNotwendig, allen Zertifikaten während der Entwicklung mit Spring @ zu vertraueWie werden ungültige SSL-Zertifikate mit Apache HttpClient behandelt?

Bitte beachten Sie, dass ich in all diesen Beispielen auch einen Cookie-Speicher und einen Proxy-Anmeldeinformationsanbieter übergebe, die ich zuvor definiert habe. Diese funktionieren, ich versuche nur, SSL-Unterstützung hinzuzufügen.

Probe # 1

Erstelle meinen eigenen SSL-Kontext mitSSLContextBuilder und vertraue allen selbst signierten Strategien mitTrustSelfSignedStrategy.

SSLContextBuilder sshbuilder = new SSLContextBuilder();
sshbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sshbuilder.build());

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .build();

ERGEBNI: Hat nicht funktioniert. BekamError while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

Probe # 2

Das gleiche wie oben, aber ein @ hinzufügPoolingHttpClientConnectionManager

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", new PlainConnectionSocketFactory())
        .register("https", sslsf)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(2000);//max connection

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .setConnectionManager(cm)
    .build();

ERGEBNI: Hat nicht funktioniert. BekamError while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

Probe # 3

Akzeptieren Sie einfach ALLE Zertifikate, indem Sie das @ überschreibeTrustStrategy (dies wird nicht empfohlen)

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
    }
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

CloseableHttpClient httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslsf)
    .build();

ERGEBNI: Hat nicht funktioniert. BekamError wh,ile trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

Probe # 4

Ich fand etwas Nützliches vondiese Antwort:

Ab Version 4.5 deaktiviert HttpClient standardmäßig die SSLv3-Protokollversion

Hier ist die Lösung, die er gab:

SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
        sslcontext, new String[] { "TLSv1", "SSLv3" }, null,
        SSLConnectionSocketFactory.getDefaultHostnameVerifier());

Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", PlainConnectionSocketFactory.INSTANCE)
        .register("https", sslConnectionSocketFactory)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

httpclient = HttpClients.custom()
    .setDefaultCredentialsProvider(credsProvider)
    .setDefaultCookieStore(cookieStore)
    .setSSLSocketFactory(sslConnectionSocketFactory)
    .setConnectionManager(cm)
    .build();

ERGEBNI: Hat nicht funktioniert. BekamError while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

Antworten auf die Frage(8)

Ihre Antwort auf die Frage