以七牛文件上传为例
mUpLoadManager为七牛上传管理类com.qiniu.android.storage.UploadManager
public final class UploadManager {
private final Configuration config;
private final Client client;
....
...
}
我们需要找到UploadManager里面的Client对象,因为OkHttpClient在他里面
package com.qiniu.android.http;
...
public final class Client {
...
private OkHttpClient httpClient;
...
}
以下是反射代码
try {
Field field1 = mUpLoadManager.getClass().getDeclaredField("client");
field1.setAccessible(true);
Client clientQN = (Client) field1.get(mUpLoadManager);
Field field2 = clientQN.getClass().getDeclaredField("httpClient");
field2.setAccessible(true);
OkHttpClient okhttpClient = (OkHttpClient) field2.get(clientQN);
Field field3 = okhttpClient.getClass().getDeclaredField("sslSocketFactory");
Field field4 = okhttpClient.getClass().getDeclaredField("certificateChainCleaner");
Field field5 = okhttpClient.getClass().getDeclaredField("hostnameVerifier");
field3.setAccessible(true);
field4.setAccessible(true);
field5.setAccessible(true);
SSLSocketFactory sslSocketFactory = SSLManager.createSSLSocketFactory();
HostnameVerifier hostnameVerifier = (s, sslSession) -> true;
field3.set(okhttpClient, sslSocketFactory);
field4.set(okhttpClient, Platform.get().buildCertificateChainCleaner(sslSocketFactory));
field5.set(okhttpClient, hostnameVerifier);
} catch (Exception e) {
e.printStackTrace();
LogUtils.e("反射修改debug包SSL认证失败");
}
import android.annotation.SuppressLint;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class SSLManager {
/**
* 创建SSLSocket的工厂类
*
* @return 返回一个SSLSocket的工厂类
*/
public static SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAllCerts() }, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return ssfFactory;
}
/**
* description: X509证书信任管理器类
*/
private static class TrustAllCerts implements X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}
网友评论