避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”異常
阿新 • • 發佈:2019-01-29
在開發https應用時,你的測試伺服器常常沒有一個(有效的)SSL證書。在你的客戶端連線測試伺服器時,如下的異常會被丟擲:”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”。
解決方案:修改建立HttpClient的方式
分析:
1.需要告訴client使用一個不同的TrustManager。TrustManager是一個檢查給定的證書是否有效的類。SSL使用的模式是X.509,對於該模式Java有一個特定的TrustManager,稱為X509TrustManager。首先我們需要建立這樣的TrustManager。
2.將TrustManager設定到我們的HttpClient。TrustManager只是被SSL的Socket所使用。Socket通過 SocketFactory建立。對於SSL Socket,有一個SSLSocketFactory。當建立新的SSLSocketFactory時,你需要傳入SSLContext到它的構造方法中。在SSLContext中,我們將包含我們新建立的TrustManager。
所以
1.建立的TrustManager
2.建立SSLContext:TLS是SSL的繼承者,但是它們使用相同的SSLContext。
3.建立SSLSocketFactory
4.將SSLSocketFactory註冊到我們的HttpClient上。這是在SchemeRegistry中完成的。
4.1.建立ClientConnectionManager
4.2.建立SchemeRegistry
5.生成HttpClient
程式碼:
import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; public class WebClientDevWrapper { public static HttpClient wrapClient(HttpClient base) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {} }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm=base.getConnectionManager(); SchemeRegistry registry = ccm.getSchemeRegistry(); registry.register(new Scheme("https", 443, ssf)); return new DefaultHttpClient(ccm, base.getParams()); /* SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", 443, ssf)); ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry); return new DefaultHttpClient(mgr, base.getParams());*/ } catch (Exception ex) { ex.printStackTrace(); return null; } } }