Android-Retrofit:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚

时间:2019-07-17 13:47:26

标签: android ssl kotlin retrofit retrofit2

我正在尝试在我的个人后端服务器上调用API,并且得到此SSL错误作为响应。

我如何要求翻新忽略SSL错误?

谢谢

2019-07-17 15:43:33.385 31394-31394/com.test E/com.test.site.ui.SiteFragment: getSitesList() - onFailure() Could not load sites list: 
    javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:229)
        at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:351)
        at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:310)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:178)
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:236)
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:109)

App.kt:

package com.test

import android.app.Application
import com.test.site.service.SiteService
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


class App : Application() {

    companion object {

        lateinit var instance: App

        // Create interceptor to get HTTP logs
        private val httpClient = OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor())
            .build();

        // Create retrofit instance for whole application
        private val retrofit = Retrofit.Builder()
            .client(httpClient)
            .baseUrl("https://api.mydomain.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        // Create siteService
        val siteService: SiteService = retrofit.create(SiteService::class.java)

    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }


}

1 个答案:

答案 0 :(得分:0)

  

我拥有后端服务器的“ pem”证书。如何将该证书与改造集成在一起?

您已经有一个自定义OkHttpClient。向其中添加逻辑以进行教学about your custom SSL certificate。然后,由于Retrofit正在使用您的OkHttpClient,因此Retrofit将知道该SSL证书,并应与您的服务器一起使用。

或者,将服务器切换为使用来自公认的根权限(例如,让我们加密)的SSL证书。