Android Glide https 自签名 双向认证

kotlin

api 'com.squareup.okhttp3:okhttp:4.9.3'

implementation 'com.github.bumptech.glide:glide:4.13.0'
    implementation 'com.github.bumptech.glide:annotations:4.13.0'
    kapt 'com.github.bumptech.glide:compiler:4.13.0'
    implementation 'com.github.bumptech.glide:okhttp3-integration:4.13.0'

老样子

<meta-data
            android:name="com.xxx.util.httpsUtil.OkHttpAppGlideModule"
            android:value="AppGlideModule" />
@GlideModule
public class OkHttpAppGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {

        LogUtils.zzz("OkHttpAppGlideModule");

//        OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
        OkHttpClient client = SafeOkHttpClient.getSafeOkHttpClient();
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
    }
}

new OkHttpUrlLoader.Factory(client)这边会报错,因为 Glide 和okhttp版本不匹配,如果想不报错,改为api 'com.squareup.okhttp3:okhttp:3.14.1',搞不懂为什么类型不一样,项目都能跑起来

OkGo 举例

//方法四:使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
        HttpsUtils.SSLParams sslParams4 = getSSLParams();
public static HttpsUtils.SSLParams getSSLParams() throws IOException {
        return HttpsUtils.getSslSocketFactory(
                TUPApplication.getApplication().getAssets().open("client.bks"),
                "xxxxx");
    }

这边用的是OkGo生成的OkHttpClient对象,如果用的不是OkGo那么就去看下 OkHttp如何生成OkHttpClient

public class SafeOkHttpClient {
    public static OkHttpClient getSafeOkHttpClient() {
        return OkGo.getInstance().getOkHttpClient();

这边我们后台给的 client.p12 证书,需要转换为 client.bks(GitHub有工具 portecle-1.11.zip ,使用最新版才可以),密码跟后台要一下,反正 https 也要走这一步

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值