0

I am trying to call a back-end API from Java Client which uses SSL authentication. For .Net there is a snippet available on how to achieve this on MS documentation, but don't have any for Java.

I have added the Application Settings - WEBSITE_LOAD_CERTIFICATES with my Private Cert Thumbprint and WEBSITE_LOAD_USER_PROFILE=1

I have tried to connect the dots and ended up with the below code but i am getting error:

      KeyStore ks;
      ks = KeyStore.getInstance("Windows-MY");
      ks.load(null, null); 
      Certificate cert = ks.getCertificate("<subject-cn>");
      TrustManagerFactory trustManagerFactory = 
      TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
      trustManagerFactory.init(ks);
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
      SSLSocketFactory factory = sslContext.getSocketFactory();
      HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
      conn.setSSLSocketFactory(factory);
      conn.setRequestMethod("GET");
      conn.setRequestProperty("Accept", "application/json");

Error::javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

What is wrong in the above code? Is there any documentation on how this can be achieved ?

AhmedVali
  • 185
  • 2
  • 16
  • Is that you use your own ssl certificate? Besides, could you please provide the document you refer to and the steps you use to configure it? – Jim Xu Dec 31 '19 at 01:25
  • Here is the link i followed - https://learn.microsoft.com/en-us/azure/app-service/configure-ssl-certificate-in-code. The certificate is provided by a Certificate Authority not brought from Azure. I have added the private certificate in the App Service and added its thumbprint in the Application Settings. – AhmedVali Dec 31 '19 at 09:10
  • Regarding the issue, you may refer to https://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun-security-validator-validatore – Jim Xu Dec 31 '19 at 09:26
  • I am running my application on Azure App Service and cant access JRE Home – AhmedVali Jan 02 '20 at 15:24

0 Answers0