我使用JSOUP连接到
许多的
https网站。我使用Jsoup方法。连接(url),但它引发异常:
javax.net.ssl.SSLHandshakeException: Certificate not valid or trusted.
因此,我使用此代码信任所有证书ssl:
public static void enableSSLSocket() throws KeyManagementException, NoSuchAlgorithmException {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType ) { }
public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType ) { }
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance( "SSL" );
sc.init( null, trustAllCerts, new java.security.SecureRandom() );
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(
new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
});
}
catch ( Exception e ) {
//We can not recover from this exception.
e.printStackTrace();
}}
但由于我使用了上面的代码,所以在游戏商店里有一个谷歌警告。
您的应用程序在Apache HTTP客户端上使用了不安全的X509TrustManager接口实现,导致了安全漏洞。有关详细信息,包括修复漏洞的截止日期,请参阅此Google帮助中心文章。
如果我添加此代码:
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
chain[0].checkValidity();
} catch (Exception e) {
throw new CertificateException("Certificate not valid or trusted.");
}
}
警告消失了,但JSOUP不工作,也不再工作,出现了相同的异常。
那么,有没有办法信任所有ssl并绕过谷歌警告?提前谢谢你。