title: How to configure Spring RestTemplate with SSL (in Spring @MVC)
body: <p>I want to configure my <strong>Spring @MVC</strong> stub application's <strong>Spring RestTemplate</strong> with <strong>SSL</strong> for communicate to <strong>REST</strong> base <strong>https</strong> application, that deployed on <strong>Tomcat</strong> server (Spring 3, Tomcat 7). I have done up to now my works by <a href="http://aruld.info/accessing-restful-services-configured-with-ssl-using-resttemplate/" rel="noreferrer">refer this link</a>. Now I have not any idea how to use these generated <strong>certificates</strong> with <strong>Spring RestTemplate</strong>, Can anyone have some idea please help me. Thanks. Up to now things I have done, </p>
  
  <p><strong>//Spring Security xml Configurations</strong></p>
  
  <pre><code>   &lt;http&gt;
      &lt;intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https"/&gt;      
      &lt;http-basic/&gt;&lt;/http&gt; 
  </code></pre>
  
  <p><strong>//Configurations for enable SSL with Tomcat</strong></p>
  
  <pre><code>&lt;Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="200"
      scheme="https" secure="true" SSLEnabled="true"
      keystoreFile="C:\Users\Channa\.keystore" keystorePass="changeit"
      clientAuth="false" sslProtocol="TLS"/&gt;
  </code></pre>
  
  <p>For generating Keys, certificates etc, </p>
  
  <p><strong>//Generate client and server keys:</strong></p>
  
  <p>F:\jdk1.6.0_23\bin>keytool -genkey -keystore keystore_client -alias clientKey -dname "CN=localhost, OU=Dev, O=MyBusiness, L=Colombo, S=Westen, C=SL"<br>
  F:\jdk1.6.0_23\bin>keytool -genkey -keystore keystore_server -alias serverKey -dname "CN=localhost, OU=Dev, O=MyBusiness, L=Colombo, S=Westen, C=SL"</p>
  
  <p><strong>//Generate client and server certificates:</strong></p>
  
  <p>F:\jdk1.6.0_23\bin>keytool -export -alias clientKey -rfc -keystore keystore_client > client.cert
  F:\jdk1.6.0_23\bin>keytool -export -alias serverKey -rfc -keystore keystore_server > server.cert</p>
  
  <p><strong>//Import certificates to corresponding truststores:</strong></p>
  
  <p>F:\jdk1.6.0_23\bin>keytool -import -alias clientCert -file client.cert -keystore truststore_server
  F:\jdk1.6.0_23\bin>keytool -import -alias serverCert -file server.cert -keystore truststore_client</p>
  
  <p><strong>//Spring RestTemplate configurations</strong> </p>
  
  <p>
      </p>
  
  <pre><code>&lt;!--Http client--&gt;
  &lt;bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"&gt;
      &lt;constructor-arg ref="httpClientParams"/&gt;
      &lt;property name="state" ref="httpState"/&gt;
  &lt;/bean&gt;
  
  &lt;!--Http state--&gt;
  &lt;bean id="httpState" class="com.org.imc.test.stub.http.CustomHttpState"&gt;
      &lt;property name="credentials" ref="usernamePasswordCredentials"/&gt;
  &lt;/bean&gt;
  
  &lt;!--User name password credentials--&gt;
  &lt;bean id="usernamePasswordCredentials" class="org.apache.commons.httpclient.UsernamePasswordCredentials"/&gt;
  
  &lt;!--Http client--&gt;
  &lt;bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory"&gt;
      &lt;constructor-arg ref="httpClient"/&gt;
  &lt;/bean&gt;
  
  &lt;!--RestTemplate--&gt;
  &lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
      &lt;constructor-arg ref="httpClientFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>//Https URL going to access</strong></p>
  
  <pre><code>ResponseEntity&lt;User&gt; rECreateUser = restTemplate.postForEntity("https://127.0.0.1:8443/skeleton-1.0/login", user, User.class);
  </code></pre>
  
  <p><strong>//Exception currently I got:</strong></p>
  
  <p>org.springframework.web.client.ResourceAccessException: I/O error: 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</p>