发布
社区首页 >问答首页 >CertPathValidatorException:未找到证书路径的信任锚点

CertPathValidatorException:未找到证书路径的信任锚点
EN

Stack Overflow用户
提问于 2017-05-05 05:48:59
回答 1查看 6.6K关注 0票数 27

我将HTTPPinning添加到OKHTTPClient中,示例代码如下:

代码语言:javascript
代码运行次数:0
复制
OkHttpClient client = new OkHttpClient();
client.setSslSocketFactory(getPinnedCertSslSocketFactory(context));


private SSLSocketFactory getPinnedCertSslSocketFactory(Context context) {
    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream incontext.getResources().openRawResource(R.raw.prod_keystore);
        trusted.load(in, "venkat@123".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) {
        Log.e("MyApp", e.getMessage(), e);
    }
    return null;
}

我把这款应用上传到了playstore,从过去的1年开始,它一直运行良好。但是从过去的1周开始,我使用了com.squareup.okhttp:okhttp:2.7.4版本的OkHttp 未找到证书路径的java.security.cert.CertPathValidatorException:信任锚点。java.security.cert.CertPathValidatorException: javax.net.ssl.SSLHandshakeException:未找到证书路径的javax.net.ssl.SSLHandshakeException信任锚点。com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357) at com.squareup.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:192) at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:149) at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:112) at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184) at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) at com.squareup.okhttp.Call.getResponse(Call.java:286) at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243) at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205) at com.squareup.okhttp.Call.execute(Call.java ):80) com.venkat.good.http.MyHTTPThread.run(MyHTTPThread.java:492) at com.venkat.good.http.MyHTTPThread.run(MyHTTPThread.java:76) at java.lang.Thread.run(Thread.java:818)

通过使用OKHTTP3,我解决了这个问题。

代码语言:javascript
代码运行次数:0
复制
String hostname = "yourdomain.com";
  CertificatePinner certificatePinner = new CertificatePinner.Builder()
 .add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
 .build();
   OkHttpClient client = OkHttpClient.Builder()
 .certificatePinner(certificatePinner)
 .build();

  Request request = new Request.Builder()
 .url("https://" + hostname)
 .build();
 client.newCall(request).execute();

,但我想知道为什么以前的OkHttp2版本工作了几天,然后又引发了这个问题?

EN

回答 1

Stack Overflow用户

发布于 2021-07-11 17:28:41

迟到总比没有

很高兴你用OkHttp3解决了你的问题。

,让我回答你问的子问题:

这是一个构建系统配置问题,而不是OkHttp的问题。每个人都惊讶地看到这样的行为,因为它应该解决OkHttp3使用所需的更高版本。如果您正在使用mavenretrofit,那么修补程序已经合并到较新的版本中(OkHttp3为上述库实现了它)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43797396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档