title: Spring boot ssl how to trust all valid certificates
body: <p>I have enabled ssl in my spring boot application and anytime I want to use a Rest service like googleapis or facebook I have to add the certificate in the truststore. </p>
  
  <p>I use openssl to get the certificate: </p>
  
  <blockquote>
    <p>openssl s_client -connect googleapis.com:443</p>
  </blockquote>
  
  <p>and I import it into the truststore : </p>
  
  <blockquote>
    <p>keytool.exe -import -noprompt -trustcacerts  -alias googleapis.com
    -file googleapis.com.cer -keystore     app-server.p12 -storepass *****</p>
  </blockquote>
  
  <p>The problem is that it's very inconvenient to manage, when the certificate expires I have to update the certificates in the truststore everytime. </p>
  
  <p>Also even if I add the right certificate sometimes I get an error: </p>
  
  <blockquote>
    <p>Caused by: 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</p>
  </blockquote>
  
  <p>I know these certificates are valid so, is there a possibility to trust them automatically without adding them in the truststore?</p>
  
  <p>Here is my ssl configuration</p>
  
  <pre><code>  ssl:
      enabled: true
      key-store: classpath:keystore/app-server.p12
      key-store-password: ******
      key-alias: app-server
      key-store-type: PKCS12
      trust-store: classpath:keystore/app-server.p12
      trust-store-password: *****
      trust-store-type: PKCS12
      keyStorePath: config/keystore/app-server.p12
  </code></pre>
  
  <p>Also I had to add system properties in the main method</p>
  
  <pre><code>private static String keyStorePath;
  
  private static String keyStorePassword;
  
  
  @Value("${server.ssl.keyStorePath}")
  public void setKeyStorePath(String keyStorePath) {
      ClientUiApplication.keyStorePath = keyStorePath;
  }
  
  @Value("${server.ssl.key-store-password}")
  public void setKeyStorePassword(String keyStorePassword) {
      ClientUiApplication.keyStorePassword = keyStorePassword;
  }
  
  public static void main(String[] args) {
  
      SpringApplication.run(ClientUiApplication.class, args);
      System.setProperty("javax.net.ssl.trustStore", keyStorePath);
      System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
  }
  </code></pre>
  
  <p>Another important detail, I need a truststore because I use self-signed certificates generated with Keytool. Without, my services cannot communicate with each other.</p>