2016-04-17 124 views
0

我的Android應用程式會告訴我,我的HTTPS證書不匹配的主機名:javax.net.ssl.SSLException:主機名的證書不匹配

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

是什麼奇怪的是,

  1. 網站(主機名1)給出了正確的證書(與瀏覽器檢查和ssllabs工具)
  2. oldhostname是以前的主機名我已經建立了在以前版本的應用程序

有沒有某種緩存的證書?我cant't找到那個

+0

您確定您正在運行具有使用'hostname1'而不是'oldhostname'的URL的代碼嗎?您的症狀適合您的Java代碼仍在使用'oldhostname'的情況。 – CommonsWare

+0

這可能是一個錯誤的URL的問題,在這種情況下,你會在瀏覽器中得到類似的錯誤。或者這是SNI的問題,請參閱http://stackoverflow.com/questions/5879894/android-ssl-sni-support。 –

+0

是的,我確定這個網址是正確的。我很確定Steffen是對的,前段時間我對SNI/Android進行了很快的瀏覽,並且我明白,對於任何高於2.x的內容,它都是可以的。看起來我誤解了我讀的內容,我會花點時間仔細閱讀鏈接。非常感謝Steffen –

回答

0

任何信息添加該類

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

,並從您的MainActivity與HttpsTrustManager.allowAllSSL();

叫它雖然它不救的方法,但我解決我的問題與此有關。

+0

有了這種「解決方法」,您在不安全的應用程序俱樂部中受到歡迎,請參閱https://www.fireeye.com/blog/threat-research/2014/08/ssl-vulnerabilities-who-listens -when-android-applications-talk.html或http://www2.dcsec.uni-hannover.de/files/android/p50-fahl.pdf。 –

+0

@SteffenUllrich是的,我在回答中提到,「這不是保存方法」,如果您有任何好的方法,請告訴我們。 – Umar

+0

正確的方法是首先找出錯誤的原因,然後修復原因,而不是通過禁用證書驗證來簡單地忽略該問題。 –

相關問題