• 关于工作中遇到的HTTPS 和 SSL


    上个星期,遇到了加密视频不能播放的问题,检查了日志发现如下报错:

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

    最终敲定原因,应该是HTTPS站点的SSL数字证书出错,导致无法建立网络连接,无法下载解密文件,也就无法播放加密视频了。

    解决方法无非两个:

    1、保持原有设计,那就是更新证书。

    2、证书不能动的 情况下,那就只能通过代码来忽略掉证书,直接建立连接了。

    关键代码如下:

    public static void trustAllHosts() {
            // Create a trust manager that does not validate certificate chains
            // Android use X509 cert
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[] {};
                }
    
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
    
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
            } };
    
            // Install the all-trusting trust manager
            try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection
                        .setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    iOS开发拓展篇—音频处理(音乐播放器5)
    iOS开发拓展篇—音频处理(音乐播放器4)
    iOS开发拓展篇—音频处理(音乐播放器3)
    iOS开发拓展篇—音频处理(音乐播放器2)
    iOS开发拓展篇—音频处理(音乐播放器1)
    iOS开发拓展篇—CoreLocation地理编码
    iOS开发拓展篇—CoreLocation定位服务
    单片机CRC源码
    VC6.0中的灰色字体是什么?
    C语言:自定义变量范围
  • 原文地址:https://www.cnblogs.com/hankzhouAndroid/p/6524896.html
Copyright © 2020-2023  润新知