OKHttp3 ----HTTPS握手失败报错:“Received fatal alert: handshake_failure“

问题产生原因:项目由JDK8升级JDK17,使用javapackager打包插件打包,部署后,发现连接https接口异常报错:报错信息

解决方案:

使用的方法:

/**
* 配置ssl socket 工厂
* @return
* @throws Exception
*/
public static SSLSocketFactory getSingleSocketFactory() throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom());
return sslContext.getSocketFactory();
}

/**
* 获取HostnameVerifier
* @return
*/
public static HostnameVerifier getHostnameVerifier() {
return (s, sslSession) -> true;
}

public static X509TrustManager getX509TrustManager() {
X509TrustManager trustManager = null;
try {
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));
}
trustManager = (X509TrustManager) trustManagers[0];
} catch (Exception e) {
e.printStackTrace();
}

return trustManager;
}

记录一下

参考文档:报sslSocketFactory(SSLSocketFactory) not supported on JDK 9+-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值