我无法使用HttpClient从SNI网站获取资源。我尝试获取的URL是:https://dabar.srce.hr/,https://www.lutrija.hr/cms/splash。
我收到此错误:
javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure
据我了解该文档,它应该像这样开箱即用(并且适用于非sni https网站):
url = "https://dabar.srce.hr/";
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// trust all certificates
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslSF).build();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpGet);
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
我试图显式启用SNIExtension:
System.setProperty("jsse.enableSNIExtension", "true");
我尝试覆盖SSLConnectionSocketFactory:
SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE) {
String targetHost = "";
@Override
public Socket createLayeredSocket(Socket socket, String target, int port, HttpContext context)
throws IOException {
this.targetHost = target;
return super.createLayeredSocket(socket, target, port, context);
}
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
try {
PropertyUtils.setProperty(socket, "host", this.targetHost);
} catch (Exception ex) {
}
super.prepareSocket(socket);
}
@Override
public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context)
throws IOException {
if (socket instanceof SSLSocket) {
try {
PropertyUtils.setProperty(socket, "host", host.getHostName());
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return super.connectSocket(connectTimeout, socket, host, remoteAddress,
localAddress, context);
}
};
我想念什么?
最佳答案
我遇到了同样的问题。这个自定义SSLConnectionSocketFactory为我修复了它:
公共类TrustAllSslSocketFactory扩展了SSLConnectionSocketFactory {
静态最终HostnameVerifier ALL_HOSTNAME_VERIFIER =(字符串主机名,SSLSession会话)-> true;
公共TrustAllSslSocketFactory(SSLContext sslContext){
超级(sslContext,ALL_HOSTNAME_VERIFIER);
}
@Override
受保护的void prepareSocket(SSLSocket socket)抛出IOException {
字符串hostName = socket.getInetAddress()。getHostName();
SNIHostName sniHostName =新的SNIHostName(hostName);
列出serverNames = new ArrayList(1);
serverNames.add(sniHostName);
SSLParameters sslParameters = socket.getSSLParameters();
sslParameters.setServerNames(serverNames);
socket.setSSLParameters(sslParameters);
}
}
见http://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#SNIExamples