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

7
在开发环境中进行测试时,我希望忽略我的开发服务器与 https证书相关的问题。
我的 Web 服务客户端出现了以下错误: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 有许多类似的问题帖子帮助我找到了解决方案,但我认为我应该在这里发布此特定问题的答案,以便需要的人能够使用....
1个回答

13
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; }
}

然后

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);

(需要 spring-ws-support 支持)


2
以上代码片段在Weblogic中部署时将无法工作,因为WL会返回weblogic.net.http.SOAPHttpsURLConnection的HTTPS连接实例,而Spring-WS期望的是javax.net.ssl.HttpsURLConnection。为了解决这个问题,您需要通过设置-DUseSunHttpHandler=true来告诉WL使用Sun的HTTP处理程序。 - SetNug

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接