美文网首页
2019-07-17 反射修改OkhttpClient的SLL

2019-07-17 反射修改OkhttpClient的SLL

作者: 兣甅 | 来源:发表于2019-07-17 11:37 被阅读0次

    以七牛文件上传为例

    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];
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:2019-07-17 反射修改OkhttpClient的SLL

          本文链接:https://www.haomeiwen.com/subject/jzqqlctx.html