Https忽略服务器证书
X509TrustManager trustAllCertManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustAllCertManager}, null);
okHttpClient.sslSocketFactory(sslContext.getSocketFactory(), trustAllCertManager);
okHttpClient.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
Https校验服务器证书
X509TrustManager trustAllCertManager = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return ACCEPT_ALL;
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
if (chain != null) {
for (X509Certificate cert : chain) {
try {
cert.verify(cert.getPublicKey());
} catch (CertificateException | InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | SignatureException ignored) {
}
}
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustAllCertManager}, null);
okHttpClient.sslSocketFactory(sslContext.getSocketFactory(), trustAllCertManager);
//Trust the cert.
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
if (hostname != null && hostname.endsWith("soyoung.com")) {
return true;
} else {
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify(hostname, session);
}
}
};
okHttpClient.hostnameVerifier(hostnameVerifier);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
抓包工具配置ca证书
https://blog.csdn.net/pingsha_luoyan/article/details/106096702
网友评论