如何在okhttp证书固定器中使用自签名的ssl

时间:2019-03-07 11:37:08

标签: ssl kotlin retrofit okhttp self-signed

我已使用以下代码将ssl证书固定到okhttp

fun getSSLClient(): OkHttpClient.Builder {
    val certificatePinner = CertificatePinner.Builder()
        .add("mydomain.com", "sha256/+yXkVIpjT60hWh4XxpPI27vC7dAPycxw7bYg/LR8Ebs=")
        .build()

    return OkHttpClient.Builder()
        .certificatePinner(certificatePinner)

}

然后我在改造中使用此方法来提供okhttp客户端的实例,如下面的代码

fun getLoginDisposable():LoginInterfaceService {
    val interceptor = HttpLoggingInterceptor()
    interceptor.level = HttpLoggingInterceptor.Level.BODY

    val client = radiSSLConfiguration.getSSLClient().addInterceptor(interceptor).build()
    val loginInterface = Retrofit.Builder().baseUrl(radiUrlConfiguration.getCloudServerUrl(this))
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .client(client)
    val loginDisposable = loginInterface.build().create<LoginInterfaceService>()
    return loginDisposable
}

但是当我通过改造提出请求时,出现以下异常

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

我应该怎么做才能删除此异常并强制android信任我的自签名ssl?我需要进行okhttp配置以信任我的自签名的ssl吗?

0 个答案:

没有答案