Trust self signed certificate in Spring Boot

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;
}

1 comment:

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete