Ignore certificate errors when requesting a URL in Java(在 Java 中请求 URL 时忽略证书错误)
问题描述
我正在尝试打印一个 URL(根本不涉及浏览器),但该 URL 当前正在抛出以下内容:
I'm trying to print a URL (without having a browser involved at all) but the URL is currently throwing the following:
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
我使用 JEditorPane 的 setPage 方法调用 URL,该方法仅将 URL 作为参数.假设我无法更改任何服务器端并且我仍然需要访问此资源,我将如何忽略证书错误(或其他使我达到目标的东西)?
I'm calling the URL using a JEditorPane's setPage method, which just takes a URL as a parameter. Assuming I can't change anything server side and I still need to get to this resource, how would I go about ignoring the certificate error (or something else that gets me to my goal)?
通过浏览器访问此 URL 告诉我该站点不受信任,并询问我是否要继续.
Accessing this URL via a browser tells me the site is untrusted and asks me if I want to proceed.
推荐答案
扩展 JEditorPane
以覆盖 getStream()
方法.
Extend JEditorPane
to override the getStream()
method.
在该方法中,您可以 打开一个 URLConnection
. 测试它是否是一个 HttpsURLConnection
.如果是,初始化 你的 拥有 SSLContext
和自定义 X509TrustManager
不执行任何 检查. 获取上下文的 SSLSocketFactory
和 将其设置为连接的套接字工厂.然后返回 InputStream
来自连接.
Inside that method, you can open a URLConnection
. Test whether it is an HttpsURLConnection
. If it is, initialize your own SSLContext
with a custom X509TrustManager
that doesn't perform any checks. Get the context's SSLSocketFactory
and set it as the socket factory for the connection. Then return the InputStream
from the connection.
这将阻止运行时为保护用户免受提供恶意软件的欺骗性站点的任何尝试.如果那是真的你想要的…
This will defeat any attempts by the runtime to protect the user from a spoof site serving up malware. If that's really what you want…
这篇关于在 Java 中请求 URL 时忽略证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中请求 URL 时忽略证书错误


基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01