【发布时间】:2018-06-08 17:13:55
【问题描述】:
当我使用 Apache 包 commons-httpclient-3.1.jar 时,我尝试使用以下逻辑来避免 ssl 认证。
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
但不幸的是,我仍然遇到错误: Https客户端证书认证错误:"javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException"
然后我选择httpclient-4.3.3.jar,上面的逻辑没用,但是如果加上下面的逻辑,就可以了。
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443));
httpClient是DefaultHttpClient的实例,socketfactory的初始化方法:
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { xtm }, null);
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
socketFactory.setHostnameVerifier(hostnameVerifier);
Scheme Register 似乎是必须的。
但是对于commons-httpclient-3.1.jar包,scheme怎么注册?
提前致谢!
【问题讨论】:
标签: ssl apache-commons-httpclient