代码之家  ›  专栏  ›  技术社区  ›  Mohit Suthar

Glide V4加载https图像

  •  6
  • Mohit Suthar  · 技术社区  · 6 年前

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

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

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

    3 回复  |  直到 5 年前
        1
  •  10
  •   chancyWu Jack BeNimble    3 年前

    我挣扎了将近1天,但找到了解决方案,如果您使用ssl认证的链接,如https,那么您需要添加两个glide依赖项,所以您必须添加此项

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

    我正在使用kotlin这就是我添加的原因 卡普特 您可以使用 注释处理器

    之后,您需要在项目中创建GlideModule,下面是 滑动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

    在我不知道的最后一步之后,这就是为什么需要一天,所以你需要 构建项目 而Glide将生成 GlideApp 类,所以您需要使用 GlideApp类显示图像HTTPS 像这样:

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

    我认为这对其他遭受这类问题的人来说是非常有帮助的。 保留代码:)

        2
  •  2
  •   Nazacheres    6 年前

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

    implementation "com.github.bumptech.glide:okhttp3-integration:$glideV"
    
        3
  •  0
  •   Nam Nguyen Thanh    5 年前

    此解决方案适用于我 实现的com。github。bumptech。滑翔:滑翔: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;
    }
    });
    
    推荐文章