javax.net.ssl.SSLProtocolException:版本1证书不再允许数据--Apache HttpClient 4.5.5

时间:2018-02-01 10:03:10

标签: java apache ssl https apache-httpclient-4.x

我正在尝试禁用ssl证书检查,但似乎没有任何效果。我正在使用Apache HttpClient 4.5.5。

这是我尝试过的最后一个解决方案:

HttpClientBuilder builder = HttpClients.custom();

System.setProperty("javax.net.ssl.trustStore", "NONE");
System.setProperty("jsse.enableSNIExtension", "false");  
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

SSLConnectionSocketFactory sslsf = null;

try {
    SSLContextBuilder sslbuilder = new SSLContextBuilder();
    sslbuilder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
    });
    sslsf = new SSLConnectionSocketFactory(sslbuilder.build(), new NoopHostnameVerifier());
} catch (Exception e) { 
    e.printStackTrace();
}

builder.setSSLSocketFactory(sslsf);

CloseableHttpClient httpclient = builder.build();

HttpGet httpget = new HttpGet("https://ru-moto.com/");

HttpResponse response = httpclient.execute(httpget);

InputStream instream = response.getEntity().getContent();

... // Here goes InputStream reading etc.

我遇到了这个例外:

javax.net.ssl.SSLProtocolException: no more data allowed for version 1 certificate
    at sun.security.ssl.HandshakeMessage$CertificateMsg.<init>(HandshakeMessage.java:452)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.upgrade(DefaultHttpClientConnectionOperator.java:193)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.upgrade(PoolingHttpClientConnectionManager.java:389)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:416)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)

...

Caused by: java.security.cert.CertificateParsingException: no more data allowed for version 1 certificate
    at sun.security.x509.X509CertInfo.parse(X509CertInfo.java:672)
    at sun.security.x509.X509CertInfo.<init>(X509CertInfo.java:167)
    at sun.security.x509.X509CertImpl.parse(X509CertImpl.java:1804)
    at sun.security.x509.X509CertImpl.<init>(X509CertImpl.java:195)
    at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:102)
    at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
    at sun.security.ssl.HandshakeMessage$CertificateMsg.<init>(HandshakeMessage.java:449)
    ... 26 more

我尝试了很多解决方案:

http://www.nakov.com/blog/2009/07/16/disable-certificate-validation-in-java-ssl-connections/

https://gist.github.com/wellsb1/bb01c625886633b29eec9444821d7f56

https://stackoverflow.com/a/44019472

https://stackoverflow.com/a/43613265

https://stackoverflow.com/a/45768724

https://memorynotfound.com/ignore-certificate-errors-apache-httpclient/

https://stackoverflow.com/a/5297100

没有任何作用,我总是得到同样的例外。

0 个答案:

没有答案