Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable SSL certificate checking with Spring WebServiceTemplate?

For testing purposes in a development environment, I want to ignore https certificate problems with my development servers.

My Web Service Client was getting :-

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

There are a number of similar Questions which helped me find the solution, but I thought I would post the answer to this specific question here for anyone who needs it....

like image 831
Neil Stevens Avatar asked Jun 25 '15 15:06

Neil Stevens


1 Answers

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

and then

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

(requires spring-ws-support)

like image 158
Neil Stevens Avatar answered Sep 22 '22 08:09

Neil Stevens