0

I am trying to call an external API that has HTTPS in it. When I invoke it from my Spring Boot Application using Rest Template I get the following error:

I/O error on POST request for "https://url-path": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I generated a self-signed certificate using keytool. I tried the following command:

sudo keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass <my-password> -validity 360 -keysize 2048

I was able to generate a file keystore.jks which I have placed in my spring boot project at the root.

My code to invoke the external API is:

CloseableHttpClient httpClient
  = HttpClients.custom()
    .setSSLHostnameVerifier(new NoopHostnameVerifier())
    .build();
HttpComponentsClientHttpRequestFactory requestFactory 
  = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);


RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<String> requestEntity = new HttpEntity<String>(getHeaders());
ResponseEntity<Object> responseEntity = restTemplate.exchange(URL_TO_CALL, 
        HttpMethod.POST,
        null, Object.class);
Object result = responseEntity.getBody();

My application.properties file has the following:

server.ssl.key-store=keystore.jks
server.ssl.key-store-password=<my-password-given-while-generating-certificate>
server.ssl.trust-store-provider=SUN

I am unable to figure out where I am going wrong.

Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78
Anamik Adhikary
  • 401
  • 1
  • 8
  • 27
  • Quick Check: Did you try the verify from: https://stackoverflow.com/questions/21076179/pkix-path-building-failed-and-unable-to-find-valid-certification-path-to-requ and https://stackoverflow.com/questions/19613562/how-can-i-specify-my-keystore-file-with-spring-boot-and-tomcat? – Nagaraj Tantri Apr 17 '21 at 17:18
  • You created a self signed certificate? Shouldn't you get the public certificate from the service you're connecting and add it to your keystore? – Vitor Santos Apr 17 '21 at 19:49

0 Answers0