首页 > 解决方案 > OkHttp3,改造和证书固定:如何使固定到期

问题描述

在我的 Android 应用程序中,我需要使用证书固定。我正在使用RetrofitOkHttp3使用 Web 服务,并且我已经定义了证书哈希码的固定。

CertificatePinner certificatePinner = new CertificatePinner.Builder()
                .add("dummy.com", "sha256/xxxxxxxxxx=")
                .build();     

OkHttpClient httpClient = new OkHttpClient.Builder()
        .certificatePinner(certificatePinner)
        .callTimeout(240, TimeUnit.SECONDS)
        .readTimeout(240, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
       .build();


Retrofit retrofitKripton = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(KriptonBinderConverterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())
        .client(httpClient).build();

我想强制证书固定直到证书到期,之后我只想避免证书固定(这是因为我想避免该应用程序在证书到期后停止工作)。有没有一种方法可以告诉OkHpttp3/Retrofit您具有所需的行为?

提前致谢

标签: androidsslokhttpcertificate-pinning

解决方案


有没有一种方法可以告诉 OkHpttp3/Retrofit 具有所需的行为?

你可以自己做:

OkHttpClient.Builder = new OkHttpClient.Builder();

if (applyPins()) {
    CertificatePinner certificatePinner = new CertificatePinner.Builder()
                    .add("dummy.com", "sha256/xxxxxxxxxx=")
                    .build();     

    builder..certificatePinner(certificatePinner);
}


OkHttpClient httpClient = builder
        .callTimeout(240, TimeUnit.SECONDS)
        .readTimeout(240, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
       .build();

Retrofit retrofitKripton = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(KriptonBinderConverterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())
        .client(httpClient).build();

实现为如果要应用引脚则applyPins()返回的方法,否则。例如,您可以使用建议的日期比较。truefalse


推荐阅读