记一次错误处理过程:HttpClient 4.3 发https请求在握手过程中报错
1、错误描述
在用HttpClient 4.3 的CloseableHttpClient访问银行接口的时候,出现如下报错:
javax.net.ssl.sslhandshakeexception:server chose sslv3, but that protocol version is not enabled or not supported by the client
对ssl连接和协议不是很了解,看字面意思是服务端使用了sslv3协议,然后客户端不支持。
2、百度解决方案
上网搜索了一下,基本上都是下面这种解决方案:
(1)、修改本机jdk安装目录/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre/lib/security下得java.security中的配置
(2)、去掉 jdk.tls.disabledAlgorithms=SSLv3, RC4, DH keySize < 768 中的 SSLv3 即可!
但是我打开对应文件,去掉sslv3,结果依旧报同样的错。
3、自行解决
网上搜不到解决方案,那就只有自己整了,从报错可以看出,是SSL握手过程报错。就先了解了一下SSL/TLS协议。
参考:
通过了解,我们大概知道问题出在哪一步,在第一次握手时,客户端会告知服务器自己支持的最高SSL协议版本号;
这里我用Wireshark捕捉了通信报文
可以看到在第一次收到Server Hello以后,客户端就返回了握手失败,查看client hello 和Server Hello里面的协议版本号如下
可以看到客户端用的是TLS 1.0 ,服务端用的是SSL 3.0。我猜想是客户端不允许用SSL 3.0协议通信,所以返回握手失败。
下面我就自定义客户端支持协议试试。
我是用HttpClientBuilder 的build方法来创建CloseableHttpClient的,我们进到源码看看它是怎么创建的
if (reuseStrategy == null) {
String[] supportedProtocols = this.systemProperties ? split(System.getProperty("https.protocols")) : null;
String[] supportedCipherSuites = this.systemProperties ? split(System.getProperty("https.cipherSuites")) : null;
X509HostnameVerifier hostnameVerifier = this.hostnameVerifier;
if (hostnameVerifier == null) {
hostnameVerifier = SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
}
if (this.sslcontext != null) {
reuseStrategy = new SSLConnectionSocketFactory(this.sslcontext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else if (this.systemProperties) {
reuseStrategy = new SSLConnectionSocketFactory((SSLSocketFactory)SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
reuseStrategy = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
}
}
从上面这段代码中,我们可以看出可以通过SSLConnectionSocketFactory来配置支持的协议,那我们就新建一个sslSocketFactory配置给HttpClientBuilder,只要修改支持协议的数组就好,其他照搬源码的默认值。
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0,false));
String[] supportedProtocols ={"TLSv1","SSLv3"};
LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
supportedProtocols,
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
CloseableHttpClient client = httpClientBuilder.build();
然后我们再发请求,发现还是不行。。。。
报错和捕捉报文情况如下
这次是服务端发送握手失败,是在客户端发完encrypted handshake message之后收到握手失败报文。去找了服务端的技术人员,但没有得到很好的答复。
后面我看到SSL 3.0在2014年的时候,Google发布在SSL 3.0中发现设计缺陷。很多大公司都禁用SSL 3.0,服务端应该不会只支持SSL 3.0协议通信吧。我猜想是否是服务端支持TLS 1.0以上的协议版本,但是不支持TLS 1.0,为了兼容客户端只能用更低版本的SSL 3.0。最后我给客户端增加支持TLS 1.1和TLS 1.2,最后握手成功!!
最后sslSocketFactory配置如下
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0,false));
String[] supportedProtocols ={"TLSv1","SSLv3","TLSv1.1","TLSv1.2"};
LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
supportedProtocols,
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
CloseableHttpClient client = httpClientBuilder.build();