2013-09-28 12 views
3

如何在新的Apache Http Client 4.3中創建SSL套接字工廠?如何在新的Apache Http Client 4.3中創建SSL套接字工廠?

這裏是我以前多麼4.3

val ts = new TrustStrategy() { 
    def isTrusted(chain: Array[X509Certificate], authType: String): Boolean = true 
} 

new SSLSocketFactory(ts, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) 

現在SSLSocketFactory標示爲淘汰,創造它。定義TrustStrategy的新方法是什麼?我無法弄清楚。

回答

5

嗯,我想通了。

初始化您ConnectionSocketFactory這樣

val sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy).useTLS().build() 
new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier()) 

如果你看一看的TrustSelfSignedStrategy來源,他們區分以假亂真自簽名證書的方式是通過檢查鏈的長度。

public boolean isTrusted(
     final X509Certificate[] chain, final String authType) throws CertificateException { 
    return chain.length == 1; 
} 

我不確定這是非常可靠的方法,但只是牢記在心。也許這是值得檢查X509CertificateisTrusted

+0

究竟是什麼意思這個空值? 「SSLContexts.custom()。loadTrustMaterial(null,...」Regards –

+0

@Zilevav它是帶有密鑰存儲(java.security.KeyStore)的對象的實例。 – expert