Trust self signed ssl certificate in Spring Boot Rest Template
public RestTemplate getRestTemplateForSelfSsl() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { TrustStrategy acceptingTrustStrategy = (X509Certificate[] x509Certificates, String s) - > true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); return restTemplate; } @ResponseBody public String send(String url, HttpEntity entity, HttpMethod httpMethod) { String response; try { try { RestTemplate restTemplate; if (url.startsWith("https")) { restTemplate = getRestTemplateForSelfSsl(); } else { restTemplate = new RestTemplate(); } ResponseEntity < String > responseEntity = restTemplate.exchange(url, httpMethod, entity, String.class); } catch (HttpStatusCodeException ex) { log.error(ex); } } catch (Exception ex) { log.error(ex); } return response; }
No comments:
Post a Comment