将.cer文件添加到okhttp

时间:2018-12-21 09:56:59

标签: android https okhttp

我在项目中使用okhttp,最近切换到了https 管理员发送了一个.cer 我试图添加它,但所有示例都使用证书中的哈希 像这样

val certificatePinner = CertificatePinner.Builder()
   .add(
           "www.example.com",
           "sha256/ZC3lTYTDBJQVf1P2V7+fibTqbIsWNR/X7CWNVW+CEEA="
   ).build()

val okHttpClient = OkHttpClient.Builder()
   .certificatePinner(certificatePinner)
   .build()

是否可以添加.cer 或如何从中获取阴影?

1 个答案:

答案 0 :(得分:1)

您希望将证书加载到您的信任库中,如OkHttp的CustomTrust.java示例:

X509TrustManager trustManager;
SSLSocketFactory sslSocketFactory;
try {
  trustManager = trustManagerForCertificates(trustedCertificatesInputStream());
  SSLContext sslContext = SSLContext.getInstance("TLS");
  sslContext.init(null, new TrustManager[] { trustManager }, null);
  sslSocketFactory = sslContext.getSocketFactory();
} catch (GeneralSecurityException e) {
  throw new RuntimeException(e);
}

client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, trustManager)
    .build();