2014-07-13 7 views
7

Ich weiß, dass es schon einmal gefragt wurde, aber ich habe alle Lösungen ausprobiert, die ich gefunden habe, und es funktioniert immer noch nicht.Apache Http Client SSL-Zertifikat Fehler

Grundsätzlich versuche ich, etwas Inhalt über Apache Http Client (4.3) zu bekommen und die Website, die ich verbinde, hat einige SSL-Probleme.

Zuerst bekam ich und SSLException mit und unrecognized_name Nachricht. Ich habe versucht, dies zu umgehen, indem ich die Eigenschaft jsse.enableSNIExtension auf false gesetzt habe.

Dann habe ich diese Ausnahme: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Ich habe dann versucht, meinen gewonnene Versorgung SSLFactory, die alle Zertifikate akzeptieren würden, aber ich bin immer noch die gleiche Ausnahme bekommen. Hier ist mein Code:

private static void sslTest() throws Exception { 
    System.setProperty("jsse.enableSNIExtension", "false"); 

    SSLContext sslContext = SSLContexts.custom() 
      .loadTrustMaterial(null, new TrustSelfSignedStrategy()) 
      .useTLS() 
      .build(); 

    SSLConnectionSocketFactory connectionFactory = 
      new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier()); 

    CookieStore cookieStore = new BasicCookieStore(); 
    HttpClientContext context = HttpClientContext.create(); 
    context.setCookieStore(cookieStore); 

    CloseableHttpClient httpclient = HttpClients.custom() 
      .setSSLSocketFactory(connectionFactory) 
      .setDefaultCookieStore(cookieStore) 
      .build(); 

    URI uri = new URIBuilder() 
      .setScheme("https") 
      .setHost(BASE_URL) 
      .build(); 

    String responseBody = httpclient.execute(new HttpGet(uri), RESPONSE_HANDLER); 
} 

Alle Hilfe wird sehr geschätzt!

+0

Bitte schauen Sie sich meine Antwort in einer anderen Frage an: https://stackoverflow.com/a/45734000/8477758 – EyouGo

Antwort

11

Bitte beachten Sie auch, dass das Vertrauen in selbstsignierte Zertifikate nicht bedeutet, einem beliebigen Zertifikat zu vertrauen. Versuchen Sie Ihr SSL-Kontext auf diese Weise einrichten:

SSLContext sslContext = SSLContexts.custom() 
     .loadTrustMaterial(null, new TrustStrategy() { 

      @Override 
      public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { 
       return true; 
      } 
     }) 
     .useTLS() 
     .build(); 

Bitte beachten Sie auch, dass in der Regel vertrauensvolle Zertifikate wahllos Niederlagen der Zweck SSL in erster Linie zu verwenden. Verwenden Sie, wenn es unbedingt notwendig oder für die Prüfung nur

+1

funktioniert wie Ein Zauber. Mit 'HttpClientBuilder' erstellen Sie so eine Instanz -' SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslContext); CloseableHttpClient client = HttpClientBuilder.create(). SetSSLSocketFactory (sslsf) .build(); ' –

1

Ihr Truststore vertraut dem Serverzertifikat nicht.

Das Zulassen aller Hostnamen ist ein HTTPS-Schritt, der nur aufgerufen werden kann wenn das Zertifikat vertrauenswürdig ist.

9

In Http-Client 4.5.2:

SSLContext sslContext = SSLContexts.custom() 
      .loadTrustMaterial(null, new TrustStrategy() { 

       @Override 
       public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { 
        return true; 
       } 
      }) 
      .build(); 

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
      sslContext, 
      NoopHostnameVerifier.INSTANCE); 

Und dann:

HttpClientBuilder builder = HttpClients.custom().setSSLSocketFactory(sslsf); 
0

Das folgende ist für Apache 4x alles

static { 
    // avoid error javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name 
    System.setProperty("jsse.enableSNIExtension", "false"); 
} 

public static HttpClientBuilder createTrustAllHttpClientBuilder() { 
    try { 
     SSLContextBuilder builder = new SSLContextBuilder(); 
     builder.loadTrustMaterial(null, (chain, authType) -> true); 
     SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE); 

     return HttpClients.custom().setSSLSocketFactory(sslsf); 
    } 
    catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { 
     throw new IllegalStateException(e); 
    } 
} 
vertrauen
Verwandte Themen