如何使用Spring WebServiceTemplate禁用SSL证书检查?

Nei*_*ens 6 java ssl spring web-services

为了在开发环境中进行测试,我想忽略https我的开发服务器的证书问题.

我的Web服务客户端得到: -

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

有许多类似的问题帮助我找到了解决方案,但我想我会在这里为任何需要它的人发布这个具体问题的答案....

Nei*_*ens 11

class UnTrustworthyTrustManager
        implements X509TrustManager
{
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
    public void checkServerTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {}
    public X509Certificate[] getAcceptedIssuers() { return null; }
}
Run Code Online (Sandbox Code Playgroud)

然后

setDefaultUri("https://myServer/soapws/ws/");
Source requestSource = new ResourceSource(new ClassPathResource("MyRequest.xml"));
StringResult result = new StringResult();
WebServiceTemplate template = getWebServiceTemplate();

HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();
sender.setTrustManagers(new TrustManager[] { new UnTrustworthyTrustManager() });
template.setMessageSender(sender);

template.sendSourceAndReceiveToResult(requestSource, result);
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)

(需要spring-ws-support)

  • 如果将其部署到 Weblogic 中,上面的代码片段将不起作用,因为 WL 将返回 HTTPS 连接的 weblogic.net.http.SOAPHttpsURLConnection 实例,而不是 Spring-WS 期望的 javax.net.ssl.HttpsURLConnection 。为了克服这个问题,您必须通过设置 -DUseSunHttpHandler=true 来告诉 WL 使用 Sun 的 HTTP 处理程序 (2认同)