java.security.cert.CertificateException: No name matching localhost found

Introduction

Here I will show you how to fix the issue java.security.cert.CertificateException: No name matching localhost found. To fix it, add a javax.net.ssl.HostnameVerifier() method to override the existing hostname verifier.

Prerequisites

Before having a look at this issue resolver, go through the tutorial using SSL with jax-ws webservice.

Resolution

Implement the following HostnameVerifier() into HelloSSlClient.java.

The complete source code is given below:

package com.roytuts.jax.ws.service.consumer;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.roytuts.jax.ws.service.Hello;
public class HelloSSLClient {
	static {
		// for localhost testing only
		javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
			public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
				if (hostname.equals("localhost")) {
					return true;
				}
				return false;
			}
		});
	}
	public static void main(String[] args) throws Exception {
		URL url = new URL("https://localhost:8443/jax-ws-webservice-tomcat/hello?wsdl");
		QName qname = new QName("http://impl.service.ws.jax.roytuts.com/", "HelloImplService");
		Service service = Service.create(url, qname);
		Hello hello = service.getPort(Hello.class);
		System.out.println(hello.sayHello("Soumitra"));
	}
}

Testing the Application

Executing the above main class you will get the following output in console:

Hello Soumitra

So it’s now working fine.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *