android - OkHttp SSLPeerUnverifiedException

标签 android ssl okhttp truststore

OkHttp 3.3.1 出现以下异常

javax.net.ssl.SSLPeerUnverifiedException: Hostname xxx not verified:
certificate: xxx
DN:xxx
subjectAltNames: []
at okhttp3.internal.io.RealConnection.connectTls(RealConnection.java:248)
at okhttp3.internal.io.RealConnection.establishProtocol(RealConnection.java:196)
at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:171)
at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at okhttp3.RealCall.getResponse(RealCall.java:243)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)

这是我的代码:

private OkHttpClient getClient() {
    OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
    builder.sslSocketFactory(getPinnedCertSslSocketFactory());
    return lBuilder.build();
  }



  private SSLSocketFactory getPinnedCertSslSocketFactory() {
    try {
      KeyStore trusted = KeyStore.getInstance("BKS");
      File trustStoreFile = new File(...);
      InputStream in = new FileInputStream(trustStoreFile);
      trusted.load(in, TRUSTSTORE_PW.toCharArray());
      SSLContext sslContext = SSLContext.getInstance("TLS");
      TrustManagerFactory trustManagerFactory = TrustManagerFactory
          .getInstance(TrustManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init(trusted);
      sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
      return sslContext.getSocketFactory();
    } catch (Exception e) {
      LOGGER.error("getPinnedCertSslSocketFactory", e);
    }
    return null;
  }

public void doRequest() {
     OkHttpClient client = getClient();
     Request request = new Request.Builder().url(URL).post(BODY).build();
     client.newCall(request).execute();
}

有什么办法解决这个问题吗?

最佳答案

builder.sslSocketFactory(getPinnedCertSslSocketFactory());

还不够。

尝试以下代码:

@SuppressLint("BadHostnameVerifier")
private static class TrustAllHostnameVerifier implements HostnameVerifier {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

builder.sslSocketFactory(getPinnedCertSslSocketFactory())
        .hostnameVerifier(new TrustAllHostnameVerifier());

关于android - OkHttp SSLPeerUnverifiedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38451981/

相关文章:

wordpress - 安装了 SSL 的 Chrome 显示 "NOT SECURE"

.net - 将自定义根与 FiddlerCore 结合使用

selenium - java.lang.NoSuchFieldError : Companion when using okhttp3 and selenium

android - 在 3G 网络中使用 Retrofit 时流被重置为 : PROTOCOL_ERROR,

android - 如何强制停止正在进行的 Intent Service?

android - Ekito 简单 KML 阅读源

ssl - 使用 apache camel 连接到 https 服务器

file - 如何将文件上传到restful api kotlin

android - 我如何学习设计这样的用户界面?

android - Google Play 控制台错误通知 'Device Support Removed'