在我的 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
具有所需的行为?
提前致谢
最佳答案
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/