【问题标题】:Disable SSL certificate validation in Java在 Java 中禁用 SSL 证书验证
【发布时间】:2019-06-28 05:14:09
【问题描述】:

如何在 java 8 中禁用证书验证。我正在尝试使用 https 连接到其他服务器,但我不断收到此错误:

Exception while providing content: [Thread[RMI TCP Connection(8)-192.168.56.1,5,RMI Runtime], 1549283885696] de.innovas.iaf.base_common.exceptions.NonRecoverableException: CT_0001_0[javax.xml.ws.soap.SOAPFaultException: Marshalling Error: com.sun.istack.SAXException2: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
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]
[Thread[RMI TCP Connection(8)-192.168.56.1,5,RMI Runtime], 1549283885696] de.innovas.iaf.base_common.exceptions.NonRecoverableException: CT_0001_0[javax.xml.ws.soap.SOAPFaultException: Marshalling Error: com.sun.istack.SAXException2: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
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 org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)
at com.sun.proxy.$Proxy511.generatePdf(Unknown Source)

我尝试使用-Dcom.sun.net.ssl.checkRevocation=false 来修复它,我找到了here。 我还尝试使用 Java Keytool 将自己的证书添加到池中。两种想法都没有改变任何东西。问题可能是我使用 openssl 生成了自己的证书。这不能由我导致错误的任何人签名。

如果我可以仅出于测试目的而禁用 SSL 检查,那就太好了。在生产场景中,我将拥有一个签名证书。

【问题讨论】:

  • 如果你不希望它安全,你为什么要使用 SSL?

标签: java ssl https


【解决方案1】:

除非仅用于测试目的,否则不建议禁用证书验证。首先你是如何调用服务的?

如果您使用的是 Apache HttpClient:

SSLContext context = SSLContext.getInstance("TLSv1.2");
TrustManager[] trustManager = new TrustManager[] {
    new X509TrustManager() {
       public X509Certificate[] getAcceptedIssuers() {
           return new X509Certificate[0];
       }
       public void checkClientTrusted(X509Certificate[] certificate, String str) {}
       public void checkServerTrusted(X509Certificate[] certificate, String str) {}
    }
};
context.init(null, trustManager, new SecureRandom());

SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context,
        SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HttpClient client = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();

如果您使用的是 HttpsURLConnection:

SSLContext context = SSLContext.getInstance("TLSv1.2");
TrustManager[] trustManager = new TrustManager[] {
    new X509TrustManager() {
       public X509Certificate[] getAcceptedIssuers() {
           return new X509Certificate[0];
       }
       public void checkClientTrusted(X509Certificate[] certificate, String str) {}
       public void checkServerTrusted(X509Certificate[] certificate, String str) {}
    }
};
context.init(null, trustManager, new SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

【讨论】:

  • 感谢您的回复。实际上问题是我真的不知道我是如何调用服务的。我知道它是一个 SOAP Webservice,但我什么都不知道。其实我宁愿不改变任何代码。如果那不可能,我必须在哪里更改您上面提到的部分?
  • 设置 JVM 属性 -Dcom.sun.net.ssl.checkRevocation=false 应该可以工作,但由于某种原因我也无法正常工作,因此我不得不修改代码,正如我在回答中解释的那样。当服务器启动时,您是否验证过该参数是否是所有 JVM 参数的一部分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2011-05-03
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
相关资源
最近更新 更多