2010-05-02 82 views
4

我一直在測試一個系統,該系統使用不同的密鑰訪問一組https服務器,其中一些是無效的,並且它們都不在我的JVM的本地密鑰存儲。我真的只是測試一下,所以我不在乎現階段的安全性。有沒有一種好的方法可以對服務器進行POST調用並告訴Java不要擔心安全證書?從Java連接到HTTPS服務器並忽略安全證書的有效性

我的谷歌搜索這帶來了一些代碼示例,使一類做驗證,永遠奏效,但我不能把它連接到任何服務器。

+2

隨着谷歌搜索的例子,你的意思是根據每個[這一個](HTTP: //www.exampledepot.com/egs/javax.net.ssl/trustall.html)? – BalusC 2010-05-02 03:03:09

+0

非常感謝BalusC。我試了3或4,編譯和運行良好,但沒有奏效。你能做出這個答案嗎? – justinhj 2010-05-02 03:50:25

+0

完成。但在將來請在正確的問題中提及正確的問題以及鏈接:) – BalusC 2010-05-02 04:31:40

回答

4

按照評論:

隨着谷歌搜索的例子,你的意思是等this one


更新:鏈路斷了,所以這裏是我從the internet archive保存相關的摘錄:

// Create a trust manager that does not validate certificate chains 
TrustManager[] trustAllCerts = new TrustManager[]{ 
    new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
     } 
     public void checkClientTrusted(
      java.security.cert.X509Certificate[] certs, String authType) { 
     } 
     public void checkServerTrusted(
      java.security.cert.X509Certificate[] certs, String authType) { 
     } 
    } 
}; 

// Install the all-trusting trust manager 
try { 
    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
} catch (Exception e) { 

} 

// Now you can access an https URL without having the certificate in the truststore 
try { 
    URL url = new URL("https://hostname/index.html"); 
} catch (MalformedURLException e) { 

} 
+0

該鏈接不再工作(域不存在)。 – Bevor 2013-11-03 17:39:16

+1

@Bevor:對。我更新了答案。 [Wayback machine](http://archive.org/web/)[有用](http://web.archive.org/web/20121022013056/http://exampledepot.com/egs/javax.net.ssl /TrustAll.html)。 – BalusC 2013-11-03 17:56:14

0

您需要創建一個繞過所有安全檢查的X509TrustManager。你可以在我回答這個問題找到一個例子,

How to ignore SSL certificate errors in Apache HttpClient 4.0

+2

然後你需要把它扔掉。編寫安全代碼然後以不安全的方式進行測試毫無意義。解決問題*。如果您不想安全,請不要使用SSL。 我不禁想到這件東西已經投入生產了多少次。 – EJP 2010-05-03 00:28:08

+1

我們這樣做! :)在生產中! – Daniel 2011-02-04 20:07:21

+2

@丹尼爾,那麼你的生產系統是不安全的。您需要在RFC2246中閱讀關於此的評論。這是一件非常嚴重的事情。 – EJP 2011-05-16 02:19:14

相關問題