我在此页面上看到了如何做https
http://sonatype.github.com/async-http-client/ssl.html
但是,如果我只想忽略并接受任何证书,该怎么办,因为在这种环境中,我现在不在乎中间的人,因为它处于孤立的环境中,而我只是在做一些QA自动测试的事情数据。
也许我的问题是如何在Java的SSL堆栈中伪造SSL,因此它在另一端接受任何证书(这不是双向的,因为它是HTTPS)。
上面链接中客户端的常见代码是
char[] keyStorePassword = "changeit".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
//ks.load(keyStoreStream, keyStorePassword);
char[] certificatePassword = "changeit".toCharArray();
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, certificatePassword);
KeyManager[] keyManagers = kmf.getKeyManagers();
javax.net.ssl.TrustManager tm = new MyTrustMgr();
javax.net.ssl.TrustManager[] trustManagers = new javax.net.ssl.TrustManager[]{tm };
SecureRandom secureRandom = new SecureRandom();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(keyManagers, trustManagers, secureRandom);
return ctx;
好吧,解决这个问题,我发现这个原因仍然不起作用
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
return ctx;
谢谢院长
聚会迟到了5年,但是我今天面临着同样的问题,您的问题在Google搜索中出现了很高的问题。所以也许我的回答会帮助别人。
使用您创建SSLContext的代码,此代码将创建一个asynchttpclient,该代码将忽略(或盲目接受)所有SSL证书:
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
.setSSLContext(createSslContext())
.build();
httpClient = new AsyncHttpClient(config);
如上所述,createsslcontext方法是精确的副本&您答案中有代码的粘贴:
private SSLContext createSslContext() throws Exception {
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] { tm }, null);
return ctx;
}
上面的示例与异步HTTP客户端1.9.40&Java 1.8