无法使用 OKHTTP3 在 IBM JRE8 上提取信任管理器 [英] Unable to Extract trust manager on IBM JRE8 with OKHTTP3

查看:146
本文介绍了无法使用 OKHTTP3 在 IBM JRE8 上提取信任管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙吗.

我也在使用 OKHTTP3 版本 4.8.1 编写 HTTP2 客户端.它适用于 Oracle JDK 8,但不适用于 IBM JRE 8.

I am also using OKHTTP3 version 4.8.1 to write HTTP2 client . Its Working on Oracle JDK 8 but not working on IBM JRE 8.

错误信息:java.lang.IllegalStateException:无法在 okhttp3.internal.Platform@e85a0ce8 上提取信任管理器,sslSocketFactory 是类 com.ibm.jsse2.SSLSocketFactoryImpl.

Error message: java.lang.IllegalStateException: Unable to extract the trust manager on okhttp3.internal.Platform@e85a0ce8, sslSocketFactory is class com.ibm.jsse2.SSLSocketFactoryImpl.

谢谢

推荐答案

你正在依赖一个长期被弃用的方法

You are relying on a long deprecated method

/github.com/square/okhttp/blob/cd722373281202492043f4294fccfe6f691ddc01/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L741

它已被弃用,因为它必须对 JVM 进行大量假设,这会在每次 JVM 更新或跨供应商时中断.您应该改为使用 X509TrustManager 作为参数调用该方法

It's deprecated because it had to assume a lot about the JVM, that breaks on each JVM update or across vendors. You should instead call the method with X509TrustManager as a parameter

/github.com/square/okhttp/blob/cd722373281202492043f4294fccfe6f691ddc01/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L767

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
    throw new IllegalStateException("Unexpected default trust managers:"
        + Arrays.toString(trustManagers));
}
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, trustManager)
    .build();

这篇关于无法使用 OKHTTP3 在 IBM JRE8 上提取信任管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆