Friday, February 12, 2016

Skip SSL certificate verification in Spring Rest Template

How to skip SSL certificate verification while using Spring Rest Template? Configure Rest Template so it uses Http Client to create requests.

Note: If you are familiar with unable to find valid certification path to requested target the below should help you.

Http Client

Firstly, import HttpClient (>4.4), to your project


Configure RestTemplate

Configure SSLContext using Http Client’s SSLContexts factory methods:

TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
        .loadTrustMaterial(null, acceptingTrustStrategy)

SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

CloseableHttpClient httpClient = HttpClients.custom()

HttpComponentsClientHttpRequestFactory requestFactory =
        new HttpComponentsClientHttpRequestFactory();


RestTemplate restTemplate = new RestTemplate(requestFactory);

org.apache.http.ssl.TrustStrategy is used to override standard certificate verification process. In the above example - it always returns true, so the certificate can be trusted without further verification.

The Test

public void opensSSLPage() throws Exception {
    String uri = "";
    ResponseEntity<String> entity = restTemplate.getForEntity(uri, String.class);

Final Word

The above code helps in certain situations (e.g. testing against servers with self-signed certificates), but it should not be used in production - unless you are 100% sure what you are doing.


  1. Yup, that'll do it, but if you want to see an example of validating the certs against keystores check out my post:

    and the partner Spring Boot demo app:

    I know it can be handy to bypass the validation but I'd always encourage leaving it as a last resort.

    1. Robin great post!!! , About this "" Do you have and example about one way ssl using Springboot?, I've seen in your examples only two way en springboot. Regards

    2. @Zilev I've replied on my blog post.

  2. Tried many things before this post of yours... Hopefully, your configuration worked beautifully! Thx

  3. Is this procedure the same as "add an exception" when we try to access an ssl secure website?
    May I make this reference?