java.security.cert.CertificateException: aucun nom correspondant à l’hôte local trouvé

Problème

Lien configuré://tomcat/comment-configurer-tomcat-au-support-ssl-ou-https/[Tomcat pour prendre en charge SSL]et déployé ce lien://webservices/jax-ws/jax-ws-hello- world-example/[service Web simple bonjour le monde]. Et utilisez la connexion client suivante au service Web déployé via une connexion SSL:

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());

    }
}

Il rencontre l’exception « Aucun nom correspondant à l’hôte local trouvé »:

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)
    ......

Solution

Ce problème et cette solution sont bien expliqués dans article , vous pouvez utiliser une solution de contournement de sécurité du transport pour votre environnement de développement « localhost ».

Pour résoudre ce problème, ajoutez une méthode javax.net.ssl.HostnameVerifier () pour remplacer le vérificateur de nom d’hôte existant, comme ceci:

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());

    }
}

Sortie

Hello World JAX-WS

Cela fonctionne bien maintenant.

lien://tag/jax-ws/[jax-ws]lien://tag/web-services/[services web]