"java.security.cert.CertificateException: No name matching localhost found"无法在 Ubuntu 16.04 LTS 上解析


  • 我正在尝试从我的Ubuntu 16.04 Lts访问远程服务器。
  • 我将远程服务器IP映射到" Localhost:60000"。
  • 我能够通过Web浏览器以及curl命令访问服务器。我会按预期得到回应。

  • 不幸的是,我无法从Java代码建立连接,这导致了以下异常,

线程中的异常" main" javax.net.ssl.sslhandshakeexception: java.security.cert.certificateException:没有匹配localhost的名称 找到

我确实在下面的链接中找到了解决方案:

  • for Ubuntu 14.04:https://www.digitalocean.com/community/community/tutorials/how-to-to-create-a-ssl-cerate-a-ssl-certificate-certificate-on-apache-on-apache-for-ubuntu-14-4-04-p->

  • for Ubuntu 16.04:https://www.digitalocean.com/community/tutorials/how-to-to-create-create-a-self-signed-signed-signed-ssl-certificate-for-apache-for-apache-in-apache-in-ubuntu-16---04

该问题在Ubuntu 14.04机器上解决,但在16.04机器上没有解决。谁能告诉我我在Ubuntu 16.04上做错了什么?

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;


TrustManager[] certs = new TrustManager[] {new X509TrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    @Override
    public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {}
    @Override
    public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {}
}};
private Client getSSLClient() throws NoSuchAlgorithmException, KeyManagementException {
    HostnameVerifier hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    ClientConfig config = new DefaultClientConfig();
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, this.certs, null);
    config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hostnameVerifier, ctx));
    Client client = Client.create(config);
    return client;
}

相关内容

  • 没有找到相关文章

最新更新