Glide加载https 的图片会出现下面异常:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.这个异常,导致图片加载不出来
解决步骤:
1./* glide */ 在glide.build 文件中的配置 implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'com.github.bumptech.glide:okhttp3-integration:4.8.0@aar' implementation 'com.github.bumptech.glide:annotations:4.8.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
在 Glide 的 Flickr 示例应用 中,有一个 AppGlideModule
的示例实现:
@GlideModule
public class FlickrGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Registry registry) {
registry.append(Photo.class, InputStream.class, new FlickrModelLoader.Factory());
}
}
请注意添加对 Glide 的注解和注解解析器的依赖:
compile 'com.github.bumptech.glide:annotations:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
最后,你应该在你的 proguard.cfg
中 keep 住你的 AppGlideModule 实现:
-keep public class extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
2.重写 AppGlideMouble 这个类,
import android.app.ActivityManager; import android.content.Context; 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.integration.okhttp3.OkHttpUrlLoader; import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.module.AppGlideModule; import java.io.InputStream; import java.security.cert.CertificateException; import java.util.concurrent.TimeUnit; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import okhttp3.OkHttpClient; /** * Created by Y * on 2019/6/26 */ @GlideModule public class HttpsGlideModule extends AppGlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); /* if (activityManager != null) { builder.setDefaultRequestOptions(ImageUtil.getOption()); }*/ } @Override public void registerComponents(Context context, Glide glide, Registry registry) { //设置请求方式为okhttp 并设置okhttpClient的证书及超时时间 OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(UnsafeOkHttpClient.getUnsafeOkHttpClient()); registry.replace(GlideUrl.class, InputStream.class, factory); } //自定义工具类修改OkHttpClient证书和超时时间 static class UnsafeOkHttpClient { public static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]); builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); builder.connectTimeout(20, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); builder.writeTimeout(20, TimeUnit.SECONDS); OkHttpClient okHttpClient = builder.build(); return okHttpClient; } catch (Exception e) { throw new RuntimeException(e); } } /** * 禁止解析Manifest文件 * 主要针对V3升级到v4的用户,可以提升初始化速度,避免一些潜在错误 * @return */ /* @Override public boolean isManifestParsingEnabled() { return false; }*/ } }
这样这样https的图片地址就可以请求了,
参考文章:https://www.cnblogs.com/suicode/p/9814002.html
https://muyangmin.github.io/glide-docs-cn/doc/configuration.html#%E9%81%BF%E5%85%8D%E5%9C%A8%E7%A8%8B%E5%BA%8F%E5%BA%93%E4%B8%AD%E4%BD%BF%E7%94%A8-appglidemodule
https://blog.csdn.net/cui130/article/details/89890915