所以我整晚都在努力让它工作,但似乎没有任何效果......我不断收到未找到证书路径的信任 anchor 。
这是我构建 okhttpClient 的方式(我遵循了 https://medium.com/@sreekumar_av/certificate-public-key-pinning-in-android-using-retrofit-2-0-74140800025b )
fun provideOkHttpClient(): OkHttpClient {
val httpClientBuilder = OkHttpClient()
.newBuilder()
val logging = HttpLoggingInterceptor()
logging.level = if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
val certificatePinner = CertificatePinner.Builder()
.add(HOST, SHA)
.build()
val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
connectionSpec.tlsVersions(TlsVersion.TLS_1_2).build()
val tlsSocketFactory = TLSSocketFactory()
return httpClientBuilder
.certificatePinner(certificatePinner)
.addNetworkInterceptor(logging)
.sslSocketFactory(tlsSocketFactory, tlsSocketFactory.systemDefaultTrustManager())
.connectionSpecs(Collections.singletonList(connectionSpec.build()))
.build()
}
我得到这样的 sha:openssl s_client -connect host:port | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -二进制| openssl enc -base64
这里是 TLSFacotry:https://gist.github.com/pollux-/fbcc74984e110bb49497faa2d0ed5ee1#file-tlssocketfactory-java
我真的不明白为什么它在那个时候不起作用..
如有任何帮助,我们将不胜感激!
最佳答案
IIRC 证书固定仅在证书链被接受后才进行评估。
拥有完整的堆栈跟踪和您尝试连接的主机会有所帮助,因为它可能使用您的客户端不接受的自签名证书。
先尝试在没有证书固定的情况下让它工作,然后再添加它。正如@CommonsWare 提到的,当证书固定失败时,它会告诉您该怎么做。
关于android - 使用 okhttp 找不到证书路径的信任 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165098/