android - OkHttp3,改造和证书固定 : how to give an expiration to the pinning

标签 android ssl okhttp certificate-pinning

在我的 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 具有所需的行为?

提前致谢

最佳答案

Is there a method to tell OkHpttp3/Retrofit to have the desired behaviour?

你可以自己做:

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() 作为一种方法,如果您想应用引脚,则返回 true,否则返回 false。例如,您可以使用建议的日期比较。

关于android - OkHttp3,改造和证书固定 : how to give an expiration to the pinning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58392677/

相关文章:

okhttp - 非法状态异常 : response is not eligible for a body and must not be closed

android - 从 Activity 开始 fragment

android - 更改进度条颜色导致进度条不显示

ssl - 了解 nginx 配置文件结构

java - Spring 启动 : One-way SSL forces key store configuration on client side

java - 安卓okhttp库

android - 无法从 android okhttp3 连接到 Node websocket

android - 如何测试不同屏幕尺寸的android应用程序?

android - 如何自定义警报对话框以使按钮适合警报对话框

asp.net-mvc - IIS Express——让 SSL 工作