Search code examples
springsslhttpsresttemplatetruststore

Unable to access HTTPS service using Spring Rest Template - TrustStore issue?


While accessing a HTTPS service I am facing the below issue :

Error: ..Certification authentication failed</TITLE> ... An attempt to authenticate with a client certificate failed. A valid client certificate is required to make this connection.

I am using Spring RestTemplate excahnge API :

restTemplate.exchange(reqInfo.getUrl(),HttpMethod.GET,requestEntity,String.class);

I tried 2 methods to provide the trustStore but still the error persists:

1.) Passing as arguments : java -cp -Djavax.net.ssl.trustStore="trustStore.jks" -Djavax.net.ssl.trustStorePassword="pwd" Test

2.) Setting the property

System.setProperty("javax.net.ssl.trustStore","path to truststore"); System.setProperty("javax.net.ssl.trustStorePassword","pwd");

Also I tried with simple Java code using HTTPclient then it works fine but with SPring RestTemplate none of option is working , am i missing something here ?

Note : If I do curl of that URL I get the same error as truststore is not provided . Hence I am assuming that this issue is due to TrustStore.


Solution

  • Finally I was able to solve the above issue .

    While building the SSL context I did not load the key store ( although I was passing it via arguments) due to which I was getting Certification authentication failed as the Key store was not available .

    Below code fixed the issue : (added loadKeyMaterial )

    sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
                                .loadKeyMaterial(keyStore, keyStorePwd).build();