前段时间项目上明明好好的定时任务,发生了崩溃,好几天的数据都没有获取到。
查了下日志发现大批量的javax.net.ssl.SSLException: Received fatal alert: protocol_version;
原因:
在Java 1.8上,默认TLS协议是v1.2。在Java 1.6和1.7上,默认是已废弃的TLS1.0
这个原因是我能找到的一个可靠的说法,但这个接口以前就是https的,我认为这个三方的服务接口应该是被做了限制。具体怎样的不得而知
解决办法:
平常我们的httpclient请求是这样的
public static CloseableHttpResponse postHttp(String url) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
response = httpclient.execute(httpPost);
return response;
}
改为,使用TLSv1.2创建httpclient的方式即可,亲测有效
public static CloseableHttpResponse getHttp(String url) throws Exception{
SSLContext ctx = SSLContexts.custom().useProtocol("TLSv1.2").build();
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(ctx)).build();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
response = httpClient.execute(httpGet);
return response;
}