问题产生原因:项目由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博客