title: java.security.cert.Certificate Exception: No Name Matching Found
body: <p>I am trying to connect to remote server(https:pathtoremotehost) using spring Rest Template and getting certificate exception. I have added the remote host's certificate to cacerts by using the following command using keytool but the error still persists</p>
  
  <pre><code>keytool -importcert -file myapp.cer -alias myapp -keystore "C:\Program Files\Java\jre1.8.0_161\lib\security\cacerts" -storepass changeit
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>@Autowired
  RestTemplate restTemplate;
  
  final String ROOT_URI = "https://&lt;&lt;path to remote host&gt;&gt;";
  
  
  
  @Override
  public Token generateToken()  {
          JSONObject request = new JSONObject();
  try {
  request.put("username","username");
  request.put("password", "password");
  } catch (JSONException e) {
  e.printStackTrace();
  }
  // set headers
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(request.toString(), headers);
  
  
  ResponseEntity&lt;Token&gt; loginResponse = restTemplate.exchange(ROOT_URI, HttpMethod.POST, entity, Token.class);
  System.out.println(loginResponse);
  return loginResponse.getBody() ;
  
  }
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>server.ssl.trust-store=C:\\Program Files\\Java\\jre1.8.0_161\\lib\\security\\cacerts
  server.ssl.key-password=changeit
  </code></pre>
  
  <p>Below is my exception</p>
  
  <pre><code>Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://&lt;&lt;pathtoremotehost&gt;&gt;/login": java.security.cert.CertificateException: No name matching &lt;&lt;remotehost&gt;&gt; found; nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching &lt;&lt;remotehost&gt;&gt; found
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)
      at com.myservice.UserServiceImpl.login(UserServiceImpl.java:88)
      at com.myservice.MyApplication.main(MyApplication.java:23)
  Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching &lt;&lt;remotehostname&gt;&gt; found
      at sun.security.ssl.Alerts.getSSLException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
      at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
      at sun.security.ssl.Handshaker.processLoop(Unknown Source)
      at sun.security.ssl.Handshaker.process_record(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
      at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76)
      at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
      at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:723)
      ... 4 more
  Caused by: java.security.cert.CertificateException: No name matching &lt;&lt;remotehost&gt;&gt; found
      at sun.security.util.HostnameChecker.matchDNS(Unknown Source)
      at sun.security.util.HostnameChecker.match(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkIdentity(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      ... 19 more
  </code></pre>