我正在使用一个使用 Apache HttpClient 的库(Dasein)。
当我尝试使用自签名证书与我的服务器建立 HTTPS 连接(通过 Dasein 和 HttpClient)时,我得到了
javax.net.ssl.SSLException: hostname in certificate didn't match
Caused by: javax.net.ssl.SSLException: hostname in certificate didn't match: <172.16.2.152> !=
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:228)
at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:149)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:130)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
我已经将证书导入到信任库并正在使用 -Djavax.net.ssl.trustStore。为了修复这个不匹配的主机名,我在我的代码中
javax.net.ssl.HostnameVerifier fakeHostnameVerifier = new javax.net.ssl.HostnameVerifier() {
@Override
public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
return true;
}
};
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(fakeHostnameVerifier);
com.sun.net.ssl.HostnameVerifier fakeSunHostnameVerifier = new com.sun.net.ssl.HostnameVerifier() {
@Override
public boolean verify(String string, String string1) {
return true;
}
};
com.sun.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(fakeSunHostnameVerifier);
当我尝试通过 直接从我的代码连接到该服务器时,由于 hostNameVerifier,一切都很好。似乎在调用 HttpClient 时它不会传播或被覆盖。new URL("https://my.server.com/api/versions")
任何想法如何解决这个问题?例如一些VM参数?
谢谢