【发布时间】:2012-08-17 02:48:03
【问题描述】:
Apache Http 客户端。可以看相关代码here:
String url = "https://path/to/url/service";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
// Test whether to ignore cert errors
if (ignoreCertErrors){
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){ return null; }
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (Exception e){
e.printStackTrace();
}
}
try {
// Execute the method (Post) and set the results to the responseBodyAsString()
int statusCode = client.executeMethod(method);
resultsBody = method.getResponseBodyAsString();
} catch (HttpException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
method.releaseConnection();
}
这是每个人都说用来忽略 SSL 证书错误的方法(仅将其设置为暂存,不会在生产中使用)。但是,我仍然收到以下异常/堆栈跟踪:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building unable to find valid certification path to requested target
任何提示都会很棒。如果我做错了 TrustManager,或者我应该以不同的方式执行 HTTP Post 方法,无论哪种方式。
谢谢!
【问题讨论】:
-
请阅读您发布的问题的答案(就像我一样,很多次)。我正在按照他说的做,但仍然遇到问题。谢谢。
-
请将您的代码粘贴为问题的一部分,以便它是独立的。像这样的小东西不需要使用 Gist/Github。
-
'Everyone' 对此是错误的。忽略证书问题会使 SSL 从根本上不安全,特别容易受到中间人攻击。请参阅RFC 2246 中的讨论。还应该注意的是,您的链接中
X509TrustManager的实现以及我看到的大多数其他链接都不符合规范。 -
@EJP。发帖人并没有要求进行安全讲座。他想知道怎么做。
标签: java jakarta-ee ssl ssl-certificate