[英]How to combine Self-signed SSL certificate and alltrusted context in Glide
https 后面有一個服務器,它使用自簽名證書提供資源。 我正在使用 okhttp/retrofit 和握手成功,所以任何資源都成功獲得。 但是,一旦我請求具有相同 Glide 但來自不同服務器的圖像,該服務器也使用 https 加密,但已經使用不同的證書,我邏輯上得到“java.security.cert.CertPathValidatorException:信任錨找不到證書路徑。”。 不幸的是,我無法為每台服務器添加證書,我將獲得的鏈接是動態的。
由於 SO 和 Google Docs 上有很多帖子,在我的 InetHelper class 中編寫了兩個函數,它們創建了兩個不同的 OkHTTPClients - 一個帶有自簽名證書,另一個用於“AllTrusted”,例如:
OkHttpClient.Builder builder = getSSLOKHttpBuilder();
okClient = builder.build();
OkHttpClient.Builder builderFree = getAllTrustedSSLOKHttpBuilder();
okAllTrustedClient = builderFree.build();
現在,我們只能在 Glide 中使用 okAllTrustedClient,在 okHttp 集成中使用 Glide,但是我們必須使用 AllTrustedClient 順便初始化 Glide 客戶端:
@GlideModule
public final class OkHttpGlideModule extends LibraryGlideModule {
private static OkHttpClient okGlideClient = InetHelper.getAllTrustedClient();
public static void setOkClient(OkHttpClient client) {
okGlideClient = client;
}
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
super.registerComponents(context, glide, registry);
registry.replace(GlideUrl.class, InputStream.class,
new OkHttpUrlLoader.Factory(okGlideClient));
}
}
但隨后它將應用於通過 Glide 收到的所有資源。 並且按需使用上面代碼中的 setOkClient function 來切換 OkHttpClient 客戶端(取決於鏈接)不起作用,顯然是由於 Glide 被初始化一次。 我想通過 Glide 僅將 AllTrusted 用於沒有注冊證書的鏈接。 在這種情況下,最好的方法是什么? 預先感謝您的任何幫助!
經過幾個小時的谷歌搜索和bumptech docs/examples閱讀,我發現我的錯誤 - 只需更改繼承的 class 和客戶參考:
@GlideModule
public final class OkHttpGlideModule extends LibraryGlideModule
至
@GlideModule
public final class OkHttpGlideModule extends AppGlideModule
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
OkHttpClient okGlideClient = InetHelper.getAllTrustedClient();
registry.replace(GlideUrl.class, InputStream.class,
new OkHttpUrlLoader.Factory(okGlideClient));
}
現在它可以工作了,但是您不能在之前覆蓋的 okHttp 客戶端之間輕松切換(在我的情況下 setOkClient 方法不起作用)。 如果有人得到它“為什么?”,請在評論中解釋。 謝謝。
希望它會有人幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.