HttpURLConnection 跳过ssl验证

简介: HttpURLConnection 跳过ssl验证

使用HttpURLConnection链接https资源时验证不通过报错如下

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
  at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
  at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
  at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
  at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
  at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
  at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
  at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
  at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
  at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
  at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
  at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
  at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)
  at com.lovnx.note.rabbitmq.listener.ThreeCallBackListener.downLoadFromUrl(ThreeCallBackListener.java:338)
  at com.lovnx.note.rabbitmq.listener.ThreeCallBackListener.main(ThreeCallBackListener.java:468)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
  at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
  at sun.security.validator.Validator.validate(Validator.java:262)
  at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
  at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
  at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
  at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596)
  ... 12 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
  at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
  at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
  at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)
  ... 18 more
Process finished with exit code 0

 

解决

public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        boolean useHttps = urlStr.startsWith("https");
        if (useHttps) {
            HttpsURLConnection https = (HttpsURLConnection) conn;
            trustAllHosts(https);
            https.setHostnameVerifier(DO_NOT_VERIFY);
        }
        //设置超时间为3秒
        conn.setConnectTimeout(3 * 1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        conn.setDoInput(true);
        conn.connect();
        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        //文件保存位置
        File saveDir = new File(savePath);
        if (!saveDir.exists()) {
            saveDir.mkdir();
        }
        File file = new File(saveDir + File.separator + fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if (fos != null) {
            fos.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        status = true;
        log.info("pdfUrl文件下载,url=[{}],保存地址=[{}],文件名=[{}]", url, savePath, fileName);
    }
/**
     * 覆盖java默认的证书验证
     */
    private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }
    }};
    /**
     * 设置不验证主机
     */
    private static final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    /**
     * 信任所有
     * @param connection
     * @return
     */
    private static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
        SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            SSLSocketFactory newFactory = sc.getSocketFactory();
            connection.setSSLSocketFactory(newFactory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oldFactory;
    }

测试

public static void main(String[] args) {
        try {
            downLoadFromUrl("https://文件路径",
                    "百度.pdf", "temp/");
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }

到此结束了,修改要获取文件的https路径即可下载

参考链接: https://www.jianshu.com/p/a928b6c34f06

目录
相关文章
|
2月前
|
Web App开发 网络安全
requests库的使用—SSL证书验证
requests库的使用—SSL证书验证
79 0
|
5月前
|
网络安全
嗯… 无法访问此页面43.139.210.211 花了太长时间进行响应,无法连接宝塔,是服务器内的宝塔面板开启了ssl的验证,但是没有绑定证书,所以被拦截,关闭宝塔面板的ssl访问认证恢复正常
嗯… 无法访问此页面43.139.210.211 花了太长时间进行响应,无法连接宝塔,是服务器内的宝塔面板开启了ssl的验证,但是没有绑定证书,所以被拦截,关闭宝塔面板的ssl访问认证恢复正常
|
网络安全 Python
requests--会话对象,ssl验证
requests--会话对象,ssl验证
|
算法 安全 网络安全
客户端如何验证ssl/tls证书的合法性
客户端是如何验证ssl/tls证书的合法性
551 1
|
Web App开发 负载均衡 安全
浏览器如何验证SSL证书及如何查看网站的证书
浏览器如何验证SSL证书及如何查看网站的证书
浏览器如何验证SSL证书及如何查看网站的证书
|
安全 网络安全 数据建模
SSL证书按照验证等级分类有哪些?
SSL证书按照验证等级分类有哪些? 按验证等级可以分为三类:
SSL证书按照验证等级分类有哪些?
|
域名解析 安全 网络协议
一文读懂,SSL证书怎么做验证?
SSL证书目前已经有越来越多的企业网站开始使用,安装SSL证书后,原有的http协议将会变成安全性更好的https加密协议,这对保护用户的信息安全,保障企业及用户的利益起着重要作用。
514 0
一文读懂,SSL证书怎么做验证?
|
安全 网络协议 网络安全
|
网络安全 PHP
PHP 5.6上的SSL证书验证
PHP 5.6上的SSL证书验证
201 0
|
安全 网络安全 数据安全/隐私保护
netty案例,netty4.1中级拓展篇十三《Netty基于SSL实现信息传输过程中双向加密验证》
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS与SSL在传输层对网络连接进行加密。
473 0
netty案例,netty4.1中级拓展篇十三《Netty基于SSL实现信息传输过程中双向加密验证》