// 在初始化OkDownload之前设置忽略证书验证
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder().sslSocketFactory(SSLHelper.getTrustAllSSLSocketFactory(), SSLHelper.getTrustAllManager()).hostnameVerifier((hostname, session) -> true);
OkDownload.Builder builder = new OkDownload.Builder(Utils.getApp()).connectionFactory((new DownloadOkHttp3Connection.Factory().setBuilder(okHttpClientBuilder)));
OkDownload.setSingletonInstance(builder.build());
SSLHelper类如下
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
public class SSLHelper {
// 获取TrustManager
public static X509TrustManager getTrustManager(InputStream certInputStream) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(certInputStream);
// 创建TrustManager并返回
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", certificate);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
return (X509TrustManager) trustManagers[0];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 获取自签名证书的SSL Socket Factory
public static SSLSocketFactory getSSLSocketFactory(InputStream certInputStream) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{getTrustManager(certInputStream)}, null);
return sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 获取一个信任所有证书的SSL Socket Factory
public static SSLSocketFactory getTrustAllSSLSocketFactory() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, null);
return sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 获取一个信任所有证书的TrustManager
public static X509TrustManager getTrustAllManager() {
return new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
}
}