title: How to disable SSL certificate checking with Spring RestTemplate?
body: <p>I am trying to write an integration test where our test launches an embedded HTTPS server using <a href="http://www.simpleframework.org/" rel="noreferrer">Simple</a>. I <a href="https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-using-java-keytool.html" rel="noreferrer">created a self-signed certificate using <code>keytool</code></a> and am able to access the server using a browser (specifically Chrome, and I do get a warning about the self-signed certificate).</p>
  
  <p>However, when I try to connect using <a href="http://docs.spring.io/spring/docs/4.0.4.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html" rel="noreferrer">Spring RestTemplate</a>, I get a <a href="http://docs.spring.io/spring/docs/4.0.4.RELEASE/javadoc-api/org/springframework/web/client/ResourceAccessException.html" rel="noreferrer">ResourceAccessException</a>: </p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8088":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:557)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502)
      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:444)
      at net.initech.DummySslServer.shouldConnect(DummySslServer.java:119)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  Caused by: 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 sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
      at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)
      at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295)
      at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1369)
      at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
      at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
      at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
      at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)
      at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
      at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541)
      ... 33 more
  Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
      at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
      at sun.security.validator.Validator.validate(Validator.java:260)
      at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
      at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1351)
      ... 47 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
      at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
      ... 53 more
  </code></pre>
  
  <p>From <a href="https://stackoverflow.com/questions/4072585/disabling-ssl-certificate-validation-in-spring-resttemplate">other questions</a> and <a href="http://www.jroller.com/jurberg/entry/using_a_hostnameverifier_with_spring" rel="noreferrer">blog</a> <a href="http://www.jroller.com/hasant/entry/no_subject_alternative_names_matching" rel="noreferrer">posts</a> I've seen the advice to replace the <code>HostnameVerifier</code> with something like </p>
  
  <pre><code>private static final HostnameVerifier PROMISCUOUS_VERIFIER = ( s, sslSession ) -&gt; true;
  </code></pre>
  
  <p>And I've set it both globally and on the <code>RestTemplate</code> itself:</p>
  
  <pre><code>HttpsURLConnection.setDefaultHostnameVerifier( PROMISCUOUS_VERIFIER );
  </code></pre>
  
  <p>...and on the <code>RestTemplate</code> itself:</p>
  
  <pre><code>final RestTemplate restTemplate = new RestTemplate();
  restTemplate.setRequestFactory( new SimpleClientHttpRequestFactory() {
      @Override
      protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
          if(connection instanceof HttpsURLConnection ){
              ((HttpsURLConnection) connection).setHostnameVerifier(PROMISCUOUS_VERIFIER);
          }
          super.prepareConnection(connection, httpMethod);
      }
  });
  </code></pre>
  
  <p>Yet, I am still getting the above error. How can I get around it? </p>
  
  <ol>
  <li>Installing the certificate locally <em>outside</em> of the unit test is <em>not</em> an option as then it would need to get installed manually on every dev machine and build server and would cause an avalanche of red tape.</li>
  <li>We need SSL since we are testing a library that sits on top of <code>RestTemplate</code> and that we are configuring it correctly.</li>
  </ol>
  
  <p>I am using Java 8 (but could use 7) and Spring 4.0.3 .</p>