【问题标题】:Using jsoup to connect to untrusted certificate使用 jsoup 连接到不受信任的证书
【发布时间】:2019-04-04 11:41:50
【问题描述】:

我的 java 应用程序有问题。我使用 JSOUP 连接到一个网站,该网站曾经是 http,现在已移至 https。现在,当我运行应用程序时,我收到以下错误,有没有人可以帮助解决这个问题?

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 32 more
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(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)

【问题讨论】:

    标签: java https sslhandshakeexception


    【解决方案1】:

    如果您信任该站点,则可以通过将其设置为 true 来忽略 http 错误:

    Document doc = Jsoup.connect("your_url").ignoreHttpErrors(true).get();
    

    若要忽略 TSL 验证,请设置 validateTLSCertificates(false):

    Document doc = Jsoup.connect("your_url").validateTLSCertificates(false).get();
    

    编辑

    此答案已过时,因为 JSoup 已弃用并删除版本 1.12.1 中的 validateTLSCertificates 方法

    (https://jsoup.org/news/release-1.12.1).

    如果您信任有问题的站点并想忽略 TLS 验证,请查看此答案how-to-resolve-jsoup-error-unable-to-find-valid-certification-path

    【讨论】:

    • 恐怕还是没有运气,继续收到同样的错误。不过还是谢谢你的建议。
    • @Danny 当您尝试忽略 TLS 验证时,它会起作用吗?
    • 可以确认,让它工作。它第一次不起作用的原因是因为我将 .ignoreHttpErrors 和 .validateTLSCertificates 链接在一起。后者解决了这个问题。谢谢你的帮助先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2010-11-15
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多