OkHttp3:SSL 证书未被识别为有效证书

OkHttp3: SSL certificate not recognized as valid certificate

我正在使用 Retrofit2RxJava2OkHttp3 对内部服务器进行 HTTPS 调用以测试 SSL 证书,但是我总是得到一个例外说明:

HTTP FAILED: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

根据 Google developer documentation,可能会出现这种情况的原因有 3 个(未知 CA、自签名证书或服务器配置缺少中间 CA)

我不能 post URL,因为它是一个内部测试服务器,但是在我的计算机或我的 phone 中的浏览器中打开 URL Chrome 工作正常,这是我从证书中看到的:

因此我认为它不能是选项 1,因为浏览器可以识别证书,只有我的应用程序和 OkHttp 似乎不是,因为它不是自签名证书,选项 2 也不应该是原因。

这就是我的 OkHttp 客户端由 Dagger 提供的方式:

@Provides
@PerApplication
OkHttpClient provideOkHttpClient(@ApplicationContext Context context) {
    int cacheSize = 10 * 1024 * 1024; // 10 MiB

    File cacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache");

    ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
            .tlsVersions(TlsVersion.TLS_1_2)
            .cipherSuites(
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
            .build();

    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
            .connectionSpecs(Collections.singletonList(spec))
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(10, TimeUnit.SECONDS)
            .cache(new Cache(cacheDirectory, cacheSize));

    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClientBuilder.addInterceptor(loggingInterceptor);
    }

    return httpClientBuilder.build();
}

我添加了 CollectionSpec 来定义我使用的是 TLS 1.2,但这没有区别,我仍然收到信任锚错误。

为了使用 ssl 证书,我还需要设置什么吗?知道为什么证书不被接受吗?

问题自行解决,服务器URL不正确