Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 463
Mohit_Kalra Avatar asked Mar 11 '26 22:03

Mohit_Kalra


1 Answers

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();
like image 187
Mohit_Kalra Avatar answered Mar 13 '26 14:03

Mohit_Kalra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!