解决融云 SDK 4.0 版本配置 https 导航报 SSLHandshakeException

解决融云 SDK 4.0 版本配置 https 导航报 SSLHandshakeException

我们公司最近使用融云 IM 进行集成开发. 我们是私有云部署, 导航是通过接口进行设置的. 是 Https 的.

之前我们是使用的2.x 版本, 使用下面接口配置一下即可.

    RongIMClient.getInstance().enableHttpsSelfCertificate(false);

但是最近我们升级融云到 4.0 版本. 突然发现连接不上了. 日志中报

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

这个错误.

于是咨询融云技术支持, 得知 4.0 需要自己进行证书验证.

需在 application 中添加即可.

 try {
    //使用X509TrustManager代替CertificateTrustManager跳过证书验证。
    TrustManager tm[] =  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 new X509Certificate[0];
                }
            }
        };
        SSLContext context = SSLContext.getInstance("TLS");   
        context.init(null, tm, null);
        SSLUtils.setSSLContext(context);
        SSLUtils.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
             }
            });
           
} catch (Throwable e) {
    throw new IllegalStateException(e);
}

注意的是, 由于导航请求是在融云的 ipc 进程中的, 所以在 application 中设置上面代码的时候, 千万不要设置在主进程中. 否则无效.

你可能感兴趣的:(android)