要在Java中跳过SSL验证,可以通过创建自定义的信任管理器来实现。下面是修改后的代码示例:

public RespBean downloadThirdFile(HttpServletResponse response) throws UnsupportedEncodingException {
    String thirdUrl ="https://dppt.tianjin.chinatax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=PDF&Jym=728E&Fphm=23122000000008542853&Kprq=20230905145917&Czsj=1693897158216"
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-Disposition", "attachment;filename=" + new String((DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".pdf").getBytes(), "ISO-8859-1"));
    if (StrUtil.isBlank(thirdUrl)) {
        return new RespBean("success", "下载失败");
    }
    if (!thirdUrl.toLowerCase().contains("http")) {
        thirdUrl = "http://" + thirdUrl;
    }
    HttpURLConnection httpUrl = null;
    BufferedInputStream in = null;
    try {
        // 创建信任管理器,跳过SSL验证
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
        };
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        URL url = new URL(thirdUrl);
        if(thirdUrl.toLowerCase().contains("https")){
            HttpsURLConnection httpsUrl = (HttpsURLConnection) url.openConnection();
            httpsUrl.setSSLSocketFactory(sslSocketFactory);
            httpUrl = httpsUrl;
        }else {
            httpUrl = (HttpURLConnection) url.openConnection();
        }
        httpUrl.connect();
        if (httpUrl.getResponseCode() != 200) {
            return new RespBean("error", "下载地址错误");
        }
        in = new BufferedInputStream(httpUrl.getInputStream());
        int len = 2048;
        byte[] bytes = new byte[len];
        while ((len = in.read(bytes)) != -1) {
            response.getOutputStream().write(bytes, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return new RespBean("error", "下载文件失败");
    } finally {
        if (httpUrl != null) {
            httpUrl.disconnect();
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            response.getOutputStream().close();
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return new RespBean("success", "下载成功");
}

在上面的代码中,我们创建了一个自定义的信任管理器 TrustManager,然后使用该信任管理器初始化了一个 SSLContext 对象,并从该对象中获取了 SSLSocketFactory。在使用 HttpsURLConnection 进行HTTPS连接时,将该 SSLSocketFactory 设置给 HttpsURLConnection 对象,这样就可以跳过SSL验证。

请注意,这种方式会跳过所有SSL验证,包括服务器证书的验证。在生产环境中,为了安全性考虑,建议仅在开发和测试环境中使用此方法。在生产环境中,应该使用有效的证书和正确的SSL验证配置

public RespBean downloadThirdFileHttpServletResponse response throws UnsupportedEncodingException String thirdUrl =httpsdppttianjinchinataxgovcn8443kpfwfpjfzzv1exportDzfpwjEwmWjgs=PDF&Jym=728E

原文地址: http://www.cveoy.top/t/topic/iZkJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录