Glide okhttps证书验证全局配置

第一步 新建AppGlideModule:

package com.guoshikeji.dramecard.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import java.io.InputStream;

/**
 * Register {@link FlickrModelLoader} for the Flickr sample app.
 */

@GlideModule
public class FlickrGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
//        builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888));
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide,
                                   @NonNull Registry registry) {
        Log.e("tyl","-----registerComponents-------");
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
    }
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }

}

第二步 AndroidManifest.xml中配置meta-data :

    <application>
   <meta-data
            android:name="com.guoshikeji.dramecard.utils.FlickrGlideModule"
            android:value="GlideModule" />
  <!--activitys />-->
   <application/>

第三步 proguard-rules.pro文件中忽略GlideModule混淆:

#GlideModule路径
-keepnames class com.guoshikeji.dramecard.utils.FlickrGlideModule
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

第四步 在继承AppGlideModule类的registerComponents方法中添加忽略证书认证:

registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
  <!--重点是这句话:new OkHttpUrlLoader.Factory()-->

重写OkHttpUrlLoader类的Factory,给OkHttpClient添加证书认证:

 public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
        private static volatile Call.Factory internalClient;
        private final Call.Factory client;
        private static Call.Factory getInternalClient() {
//            glide添加双向认证
            OkHttpClient.Builder okHttpClient = new OkHttpClient().newBuilder();
            okHttpClient.hostnameVerifier(new Home());//忽略证书域名不受信任问题
           okHttpClient.sslSocketFactory(MySSLSocketFactory.getSocketFactory(MyApplication.getInstance()));//添加证书
            OkHttpClient build = okHttpClient.build();
            if (internalClient == null) {
                synchronized (Factory.class) {
                    if (internalClient == null) {
                        internalClient = build;
                    }
                }
            }
            return internalClient;
        }
  private static class Home implements HostnameVerifier {
            public SSLSession sslSession;

            @Override
            public boolean verify(String hostname, SSLSession session) {
                this.sslSession = session;
                return true;
            }
        }

添加证书的MySSLSocketFactory类代码可复制https://www.jianshu.com/p/6229d10d3550中的SSLSocketFactory类和X509TrustManager 这2个类即可!

各类知识点整理:

工具类:

源码: