2013-05-31 71 views
1

我目前正在開發一個Android應用程序上,我需要做連接到測試服務器的SSLSocket。的SSLSocket連接信任證書

的是,我們不會買一個證書,該服務器,因爲它是一個測試服務器,而不是生產。

雖然它可以在模擬器罰款,但是在我的S3它給我這個錯誤:

06-01 00:36:17.355: I/System.out(13629): java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. 

唯一的例外是拋出這個代碼的最後一行:

mSf = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
mSocket = (SSLSocket) mSf.createSocket("myTestingServer.com", 443); 
mSocket.startHandshake(); 

我不我真的很關心解決方案是代碼還是降低安全檢查作爲Android用戶,因爲它是在我的S3上進行測試,一旦在生產中,證書應該是好的。 這不是一個HTTP服務器,所以我不能使用org.apache.http.org.ssl

+0

檢查你的過期日期和時間實在是太舊,請正確設置它和嘗試。 – pyus13

+0

不是我的問題,我現在無法訪問服務器,我無法更改任何內容。無論證書如何,我只需要連接。 – TheSquad

回答

0

也許你想忽略證書檢查

警告:此代碼是不是安全

TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { 
    public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
     return null; 
    } 

    @Override 
    public void checkClientTrusted(X509Certificate[] certs, String authType) { 
    } 

    @Override 
    public void checkServerTrusted(X509Certificate[] certs, String authType) { 
    } 
}}; 

// Install the all-trusting trust manager 
SSLContext sc = SSLContext.getInstance("SSL"); 
sc.init(null, trustAllCerts, new java.security.SecureRandom()); 

SSLSocketFactory sslsocketfactory = sc.getSocketFactory(); 
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("google.com", 443); 
... 

來源:Using Java to connect with SSLSocket, trusting all certificates