【问题标题】:Glide V4 load https imagesGlide V4 加载 https 图像
【发布时间】:2018-09-08 11:17:45
【问题描述】:

我知道这个link,并尝试过,但这是针对 Glide V3 解决方案,我需要加载 https://myimage/image/xxx.png 但 glide 抛出异常

FileNotFoundException(No content provider)** and **SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found

我使用的是 4.5.0 Glide 版本, 我花了很多时间但找不到任何解决方案,任何帮助将不胜感激。

【问题讨论】:

  • 这个异常意味着你的 myimage 服务器有一个自签名证书,而不是一个公开签名的证书。下面的答案解释了如何告诉 glide 忽略这一点。另一种选择是正确设置 https,例如通过在您的 Web 服务器上安装 let's encrypt 或将图像托管在 S3 或其他配置良好的环境中......

标签: android android-glide sslhandshakeexception image-loading


【解决方案1】:

我在 1 天左右苦苦挣扎,但找到了解决方案,如果您使用像 https 这样的 ssl 认证链接,那么您需要再添加两个 glide 依赖项,因此您必须添加这个

implementation 'com.github.bumptech.glide:annotations:4.5.0'
kapt 'com.github.bumptech.glide:compiler:4.5.0'

我使用的是 kotlin,所以我添加了 kapt 你可以使用 annotationProcessor

之后需要在项目中创建GlideModule,下面是Glide V4

的代码
 @GlideModule
public class MyGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        super.applyOptions(context, builder);
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        OkHttpClient okHttpClient= UnsafeOkHttpClient.getUnsafeOkHttpClient();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
    }
}

这里我们绕过了 ssl,所以这里是 UnsafeOkHttpClient

    public 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;
                }
            });

            OkHttpClient okHttpClient = builder.build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 }

之后你还需要两个类 OkHttpUrlLoader 和 OkHttpStreamFetcher

你可以从Glide Link复制过去

在最后一步之后,我不知道这就是为什么它需要一天的时间,所以你需要 build project 并且 Glide 会生成 GlideApp 类,所以你需要使用该 GlideApp 类来显示图像 HTTPS 像这样:

GlideApp.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView)

我认为这对于遇到此类问题的其他人来说非常有帮助。 保留代码:)

【讨论】:

  • 您可能会遇到游戏市场验证问题
  • 我有问题 Glide:有 1 个原因:java.net.UnknownHostException(host == "null)" 在某些设备上,我尝试按照您的回答,它有效。谢谢。
  • @HuoChhunleng 哦!太好了,这是我的荣幸。
  • 在构建图像请求时不要忘记使用GlideApp 而不是“Glide”
  • 这是 Glide 从带有 https 的 url 加载图像的最佳解决方案,谢谢。
【解决方案2】:

Mohit 答案的结构很好,但是现在你可以参加所有必需的课程here 所以要包含它们,只需添加

implementation "com.github.bumptech.glide:okhttp3-integration:$glideV"

【讨论】:

    【解决方案3】:

    此解决方案适用于我 实现 'com.github.bumptech.glide:glide:4.8.0'

    添加扩展应用程序

    SSLContext mySSLContext = SSLContext.getInstance("TLS");
    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    return new java.security.cert.X509Certificate[]{};
    }
    
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }
    }};
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String arg0, SSLSession arg1) {
    if (arg0.equalsIgnoreCase("YOUR_DOMAIN OR YOUR IP"))
    return true;
    else
    return false;
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多