Using Spring RestTemplate with Client Certificate

To configure Spring RestTemplate to use a client certification in a HTTPS connection, you can follow these steps:

  1. Defines and loads a KeyStore to store a client certificate. In this example, the certificate stored in a PKCS12 archive.
  2. Defines a SSLContextBuilder to custom and build the SSL context to be used with the RestTemplate. In this example, we need to connect to the host with self-signed certificate, so we have to load TrustSelfSignedStrategy() in the loadTrustMaterial() method.
  3. Defines a SSLConnectionSocketFactory with the SSLContextBuilder from step 2.
  4. Defines a ClosableHttpClient using HttpClients builder class and set SSLSocketFactory using SSLConnectionSocketFactory from step 3.
  5. Defines a HttpComponentsClientHttpRequestFactory using ClosableHttpClient  from step 4, and configure timeout values.

  6. Finally, create a RestTemplate using HttpComponentsClientHttpRequestFactory from step 5.
  7. To summarize.