java - 如何在运行 groovy 脚本 URL 时禁用 SSL 验证?

标签 java security ssl groovy jenkins-groovy

我正在尝试运行一个本地 groovy scipt 进行测试,它在 prod 服务器上运行良好,但在本地我收到 SSL 错误:

Caught: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at java_net_URLConnection$getOutputStream$1.call(Unknown Source)
    at url.processRequest(url.groovy:8)
    at url$processRequest.callCurrent(Unknown Source)
    at url.run(url.groovy:17)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    ... 7 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    ... 7 more
我可以采取什么解决方法来删除本地 groovy 脚本中的 SSL 验证? whihc 行给出错误:
def post = new URL("https://abc.deg.com").openConnection();

最佳答案

您可以将连接强制转换为 HttpsUrlConnection 并注入(inject)不验证任何内容的不安全信任管理器。这样,您将删除所有 SSL 验证。我不推荐这样做,因为它不安全,但这是您可以使用的代码段:
不安全信任管理器

import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLEngine
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.TrustManager
import javax.net.ssl.X509ExtendedTrustManager
import java.security.cert.CertificateException
import java.security.cert.X509Certificate

class UnsafeTrustManager extends X509ExtendedTrustManager {

    @Override
    void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {}

    @Override
    void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException {}

    @Override
    void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {}

    @Override
    void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException {}

    @Override
    void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}

    @Override
    void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}

    @Override
    X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0]
    }

}
用法
def sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, new TrustManager[]{new UnsafeTrustManager()}, null)
def sslSocketFactory = sslContext.getSocketFactory()

def post = (HttpsURLConnection) new URL("https://abc.deg.com").openConnection()
post.setSSLSocketFactory(sslSocketFactory)

关于java - 如何在运行 groovy 脚本 URL 时禁用 SSL 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68167184/

相关文章:

java - 我如何使用这个 sql 查询来 hibernate 条件函数

java - 将 'Web, 28 Aug 2014 15:47' 格式的日期转换为正确的格式

java - 带有数组的标记存在语法错误

facebook - iframe 中的 SSL 页面和非安全内容?

node.js - 从没有证书的 Node 套接字提交 HTTP

node.js - 使用来自拥有的域的 TLS 证书,并在 AWS Elastic Beanstalk 提供的 URL 上使用它

java - 将二进制文件中的数组输出到程序

java - 通过 HTTP 保护 Spring Data RepositoryRestResource (CrudRepository),但不是在内部

php - 加密用于票务系统的条形码上显示的 ID 的值

flash - 在可扩展的 Flash 横幅中使用表单?