title: How java validate certificate and why i get SSLHandshakeException
body: <p>I'm trying to call an api using RestTemplte from service on server A and the other service on server B and i get the error below : </p>
  
  <pre><code>sun.security.validator.ValidatorException: 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: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  </code></pre>
  
  <p>Even when i added this configuration (code below) into the rest template still the same problem.</p>
  
  <pre><code>SSLContext sslContext = new SSLContextBuilder()
                  .loadTrustMaterial(new File(keystore), trustStorePassword.toCharArray())
                  .build();
          SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
          HttpClient httpClient = HttpClients.custom()
                  .setSSLSocketFactory(socketFactory)
                  .build();
          HttpComponentsClientHttpRequestFactory factory =
                  new HttpComponentsClientHttpRequestFactory(httpClient);
          RestTemplate restTemplate = new RestTemplate(factory);
  </code></pre>
  
  <p>the solution that i found on the net is to export certificate from chrome and add it to the JVM trusted certificate. I don't know if this is the right solution becouse we can change this certif evry 3 month for example.. </p>
  
  <p>And i have some other questions : </p>
  
  <ul>
  <li><p>Does JVM knows all certificate authority (i think there are billion..) for example when i call google using rest certificate will be validated by JVM using "security/cacerts"? </p></li>
  <li><p>Should I add our certificate in JVM trusted certifies to fix <code>sun.security.validator.ValidatorException: 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: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target</code>? </p></li>
  <li><p>The validation process is not automatic by SSL mechanism ?</p></li>
  </ul>
  
  <p>thank you in advance.</p>