Sunday, 21 August 2016

Rest Template Spring and Injecting SSL Bean for HTTPS


Requirement is to have a common service to make rest call with a third party api . To have the configuration on  HTTPS we basically require to have certificates initialized to out SSL context , (Previous post on how to setup your ssl bean in spring context)

Register the autowired bean in your class . 

 @Autowired(required=false)
 private MySSLSocketFactory socketFactory; 
  
              
Code to make the rest call in production (HTTPS)  environment.

          
   sslContext = socketFactory.getSSLContext();
   SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
   CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
   HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
   requestFactory.setHttpClient(httpClient);
   restTemplate.setRequestFactory(requestFactory);
   HttpEntity> restRequest = new HttpEntity>(map, createHeaders("userId","userPwd")));
   ResponseEntity response= restTemplate.postForEntity(URL, restRequest , String.class );

AWS Certificate Manager - Import Certificate

How to import a certificate. Use Key Store Explorer to open the cert and export keypair in PEM format  This will save a file cert.pem. Make ...