java.security.cert.CertificateException: Es wurde kein Name gefunden, der mit localhost übereinstimmt

Problem

Konfigurierter Link://tomcat/how-to-configure-tomcat-zu-support-ssl-or-https/[Tomcat zur Unterstützung von SSL]und Bereitstellung dieses Links://webservices/jax-ws/jax-ws-hello world-example/[einfaches hallo world web service]. Verwenden Sie den folgenden Client, um eine Verbindung zum bereitgestellten Webdienst über eine SSL-Verbindung herzustellen:

package com.mkyong.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{

    public static void main(String[]args) throws Exception {

    URL url = new URL("https://localhost:8443/HelloWorld/hello?wsdl");
        QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        System.out.println(hello.getHelloWorldAsString());

    }
}

Es wird folgende Ausnahme angezeigt: " Kein Name für localhost gefunden ":

Caused by: javax.net.ssl.SSLHandshakeException:
    java.security.cert.CertificateException: No name matching localhost found
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1611)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
    ......
Caused by: java.security.cert.CertificateException: No name matching localhost found
    at sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:210)
    at sun.security.util.HostnameChecker.match(HostnameChecker.java:77)
    ......

Lösung

Dieses Problem und diese Lösung werden in diesem Artikel unter article ausführlich beschrieben. Sie können einen Transport Security (SSL) -Umgang verwenden für Ihre " localhost " Entwicklungsumgebung.

Um dies zu beheben, fügen Sie eine javax.net.ssl.HostnameVerifier () - Methode hinzu, um den vorhandenen Hostnamen-Verifizierer wie folgt zu überschreiben:

package com.mkyong.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{

    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/HelloWorld/hello?wsdl");
        QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        System.out.println(hello.getHelloWorldAsString());

    }
}

Ausgabe

Hello World JAX-WS

Es funktioniert jetzt gut.