title: How to avoid javax.net.ssl.SSLPeerUnverifiedException: Hostname test.server.com not verified
body: <p>I have a spring-boot app (version 2.3.7) that uses okhttp3 and I'm trying to call an api in <code>https://test.server.com:8888/api</code> . The certificate on the server is self-signed so I have updated my carets to trust this certificate. When I m running the SSLPoke.class like this <code>java SSLPoke test.server.com 8888</code> I m getting <code>Successfully Connected</code>. But when the request is made from my app I m getting the <code>javax.net.ssl.SSLPeerUnverifiedException: Hostname test.server.com not verified:</code> error. Since the cacerts is updated do I need to write extra code to verify the hostname ?</p>
  <p>To be more specific I have the following snippet. If I pass the truststore in the VM options <code>-Djavax.net.ssl.trustStore=client-truststore.jks -Djavax.net.ssl.trustStorePassword=changeit</code> the call with the okhttp fails, but with rest template succeeds.</p>
  <pre><code>@SpringBootApplication
  public class DemoOkhttpClientApplication implements CommandLineRunner {
  
      public static void main(String[] args) {
          SpringApplication.run(DemoOkhttpClientApplication.class, args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          try {
              Retrofit retrofit = new Retrofit.Builder()
                      .baseUrl(&quot;https://localhost:8443/&quot;)
                      .addConverterFactory(MoshiConverterFactory.create())
                      .build();
              TestClient testClient = retrofit.create(TestClient.class);
              System.out.println(String.format(&quot;OkHttp Response %s&quot;, testClient.callTestEndpoint().execute().body()));
          } catch (Exception ex){
              System.err.println();
              System.err.println(&quot;Ok Http error &quot; + ex.getMessage());
          }
  
          try {
              RestTemplate restTemplate = new RestTemplate();
              System.out.println(String.format(&quot;Rest Template Response %s&quot;,
                      restTemplate.exchange(&quot;https://localhost:8443/test&quot;, HttpMethod.GET, null, String.class)));
          } catch (Exception ex){
              System.err.println(&quot;Rest Template error&quot; + ex.getMessage());
          }
      }
  }
  
  interface TestClient {
      @GET(&quot;/test&quot;)
      Call&lt;String&gt; callTestEndpoint();
  }
  
  </code></pre>
  <p>The output of the above snippet is this</p>
  <pre><code>Ok Http error Hostname localhost not verified:
      certificate: sha256/INkKXJiMFIGNnvE5ga1Ye0KjxjP5jO9hIrNvQs4wuU0=
      DN: CN=localhost, OU=PC, O=PC, L=Marousi, ST=Athens, C=GR
      subjectAltNames: []
  Rest Template Response &lt;200,Ok,[Content-Type:&quot;text/plain;charset=UTF-8&quot;, Content-Length:&quot;2&quot;, Date:&quot;Thu, 11 Feb 2021 17:09:55 GMT&quot;, Keep-Alive:&quot;timeout=60&quot;, Connection:&quot;keep-alive&quot;]&gt;
  
  </code></pre>