Java8, HttpClient, receiving “Received fatal alert: handshake_failure”

余生长醉 提交于 2019-12-05 03:00:54
ZhongYu

According to ssllabs, the server is all right, and it works java7+. Notably, clients that don't support SNI will fail talking to the server. In your debug dump, SNI is indeed missing, and that's likely the problem.

SNI should be enabled by default on java7+. Your stacktrace shows that indeed it's on java8. This link might help you.

We experience the same issue with httpclient and java 8 trying to access an amazon server with https.

Seems to be related to this bug :

https://bugs.openjdk.java.net/browse/JDK-8072464

This is an openJDK bug but we also experience it with Oracle 8u60

For those arriving late here, like me:

The problem happens because of the issue Server Name Indication (SNI) Support

But this issue was already fixed, so what's the problem? The problem happens because you are using the deprecated SSLSocketFactory instead of the newer SSLConnectionSocketFactory.

The issue was fixed inSSLConnectionSocketFactory but not in the deprecated SSLSocketFactory.

So, if you can, use SSLConnectionSocketFactory and you should be fine.

SSLSocketFactory and SSLConnectionSocketFactory are obviously not interchangeable, and if you are like me, you may have a lot of code to change before you can replace one with the other, so what do we do?

What I did: I extended SSLSocketFactory, and overrided a method in a way to match the patch that fixed the issue:

public class PatchedSSLSocketFactory extends SSLSocketFactory {
    public PatchedSSLSocketFactory(
        final SSLContext sslContext,
        final X509HostnameVerifier hostnameVerifier) {
        super(sslContext, hostnameVerifier);
    }

    @Override
    public Socket createSocket(final HttpContext context) throws IOException {
        return SocketFactory.getDefault().createSocket();
    }
}  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!