springvalidationssl-certificateresttemplate

Disabling SSL Certificate Validation in Spring RestTemplate


I am having two Spring-based web apps A and B, on two different machines.

I want to make an HTTPS call from web app A to web app B, however, I am using a self-signed certificate in Machine B. So my HTTPS request fails.

How can I disable HTTPS certificate validation when using RestTemplate in Spring? I want to disable validation because both web app A and B are within the internal network, but data transfer has to happen over HTTPS


Solution

  • What you need to add is a custom HostnameVerifier class bypasses certificate verification and returns true

    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });
    

    This needs to be placed appropriately in your code.