美文网首页
OKHTTP sslsocketfactory 过期,解决方案

OKHTTP sslsocketfactory 过期,解决方案

作者: GODANDDEVIL | 来源:发表于2020-03-13 14:02 被阅读0次

    报错:clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+
    原因是单参数的 构造函数 被弃用。

                try {
                    TrustManagerFactory trustManagerFactory = null;
                    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 okHttpClient = new OkHttpClient.Builder()
                            .sslSocketFactory(sslSocketFactory, trustManager).build()
                    }catch(Exception e){}
    
    

    在遇到双向证书的情况下,需要指定keymanager 以及TrustManager 如微信支付,发生企业支付,企业转账的时候。对上述代码进行更改如下

    try {
                TrustManagerFactory trustManagerFactory = null;
                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];
     
                KeyStore keystore = KeyStore.getInstance("PKCS12");
                char[] keyPassword = tradeNumber.toCharArray();
                //证书密码
                keystore.load(resource.getInputStream(), keyPassword);
                SSLContext wx_ssl_context = SSLContext.getInstance("TLS");
                KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                kmfactory.init(keystore, keyPassword);
                wx_ssl_context.init(kmfactory.getKeyManagers(), new TrustManager[]{trustManager}, new SecureRandom());
                SSLSocketFactory sslSocketFactory = wx_ssl_context.getSocketFactory();
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .sslSocketFactory(sslSocketFactory, trustManager).connectionPool(new ConnectionPool(300, 3, TimeUnit.MINUTES)).build();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    

    转自:https://blog.csdn.net/topc2000/article/details/104032485/

    相关文章

      网友评论

          本文标题:OKHTTP sslsocketfactory 过期,解决方案

          本文链接:https://www.haomeiwen.com/subject/eajhshtx.html