android - OkHttp3,改造和证书固定:如何使固定到期
问题描述
在我的 Android 应用程序中,我需要使用证书固定。我正在使用Retrofit
并OkHttp3
使用 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
您具有所需的行为?
提前致谢
解决方案
有没有一种方法可以告诉 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()
返回的方法,否则。例如,您可以使用建议的日期比较。true
false
推荐阅读
- xml - xml 上的自加入慢
- jboss - java.lang.IllegalArgumentException: UT000068: Servlet 路径匹配失败
- javascript - 需要将 Web Form JavaScript 转换为 js 文件
- graph - 如何开发时序数据库的数据可视化?
- java - 测试 spring 批处理作业导致意外结果
- python - 如何从表 python 中刮取特定的 tr 或 td
- php - 注册后的laravel网站有时会卡在控制器中
- javascript - 如何在像 babylon.js-NulEngine 这样的节点服务器上设置无头版本的 three.js?
- firebase-cloud-messaging - Firebase 控制台云消息传递 - 服务器时间不是收件人
- python - 如何从创建的函数中打印参数?