简体   繁体   English

javax.net.ssl.SSLException:证书中的主机名不匹配

[英]javax.net.ssl.SSLException: hostname in certificate didn't match

My Android app tells me that my https certificate doesn't match the hostname: 我的Android应用程序告诉我我的https证书与主机名不匹配:

javax.net.ssl.SSLException: hostname in certificate didn't match: <hostname1> != <oldhostname>

What is odd is that 奇怪的是

  1. The website ( hostname1 ) gives the correct certificate (checked with browsers and the ssllabs tool) 网站( hostname1 )提供正确的证书(已通过浏览器和ssllabs工具进行检查)
  2. oldhostname is the previous hostname I had set up in previous versions of the app oldhostname是我在该应用程序的先前版本中设置的先前主机名

Is there some kind of cache for certificates? 证书是否有某种缓存? I cant't find any info on that 我找不到任何相关信息

Add this class 新增课程

public class HttpsTrustManager implements X509TrustManager {
    private static TrustManager[] trustManagers;
    private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

    @Override
    public void checkClientTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {
    }

    @Override
    public void checkServerTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {
    }

    public boolean isClientTrusted(X509Certificate[] chain) {
        return true;
    }

    public boolean isServerTrusted(X509Certificate[] chain) {
        return true;
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return _AcceptedIssuers;
    }

    public static void allowAllSSL() {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }

        });

        SSLContext context = null;
        if (trustManagers == null) {
            trustManagers = new TrustManager[]{new HttpsTrustManager()};
        }

        try {
            context = SSLContext.getInstance("TLS");
            context.init(null, trustManagers, new SecureRandom());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }

        HttpsURLConnection.setDefaultSSLSocketFactory(context != null ? context.getSocketFactory() : null);
    }
}

and call it from your MainActivity with HttpsTrustManager.allowAllSSL(); 并使用HttpsTrustManager.allowAllSSL();从您的MainActivity调用它HttpsTrustManager.allowAllSSL();

Although it's not save approach but i solve my problem with this. 虽然这不是节省方法,但是我用这个解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestEasy的客户端实现导致javax.net.ssl.SSLException:证书中的主机名不匹配 - RestEasy's Client implementation causes javax.net.ssl.SSLException: hostname in certificate didn't match javax.net.ssl.SSLException:证书中的主机名与Apache httpd虚拟主机不匹配 - javax.net.ssl.SSLException: hostname in certificate didn't match in conjunction with Apache httpd virtual hosts 间接Apache HttpClient和javax.net.ssl.SSLException:证书中的主机名不匹配 - Indirect Apache HttpClient and javax.net.ssl.SSLException: hostname in certificate didn't match 使用HTTPS Confusion的Java HTTP帖子 - javax.net.ssl.SSLException:证书中的主机名不匹配 - Java HTTP post using HTTPS Confusion - javax.net.ssl.SSLException: hostname in certificate didn't match org.jolokia.client.exception.J4pException:与服务器联系时出现IO错误:javax.net.ssl.SSLException:证书中的主机名不匹配 - org.jolokia.client.exception.J4pException: IO-Error while contacting the server: javax.net.ssl.SSLException: hostname in certificate didn't match javax.net.ssl.SSLException:<>的证书与任何主题备用名称都不匹配:[] - javax.net.ssl.SSLException: Certificate for <> doesn't match any of the subject alternative names: [] javax.net.ssl.SSLException:证书与任何主题备用名称都不匹配 - javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names Android中的“javax.net.ssl.SSLException:不受信任的服务器证书”异常 - “javax.net.ssl.SSLException: Not trusted server certificate” exception in Android Java SSLException:证书中的主机名不匹配 - Java SSLException: hostname in certificate didn't match GWT:Tomcat无法序列化“ javax.net.ssl.SSLException” - GWT: Tomcat can't serialize 'javax.net.ssl.SSLException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM