Glide V4加载https图像

时间:2018-03-29 13:15:28

标签: android android-glide sslhandshakeexception image-loading

我知道这个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 个答案:

答案 0 :(得分:1)

我在接近1天左右挣扎但是找到解决方案,如果你使用像https这样的ssl认证链接,那么你需要再添加两个滑动依赖,所以你必须添加这个

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

复制过去

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

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

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

答案 1 :(得分:0)

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

def get(self, request):
    form = xx[![enter image description here][1]][1]Form()
    FW_list = FW.objects.all().order_by('approve_time').reverse()        
    return render(self.request, self.template_name, locals())

答案 2 :(得分:0)

此解决方案使我与 实施'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;
}
});