修正SSL错误

This commit is contained in:
jinyu
2015-08-19 14:14:57 +08:00
parent daba44fd02
commit 48086312b6
9 changed files with 102 additions and 197 deletions
@@ -31,7 +31,7 @@ import org.apache.commons.httpclient.methods.TraceMethod;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
import com.foxinmy.weixin4j.http.AbstractHttpClient;
import com.foxinmy.weixin4j.http.HttpClient;
@@ -97,10 +97,8 @@ public class HttpComponent3 extends AbstractHttpClient implements HttpClient {
}
SSLContext sslContext = params.getSSLContext();
if (sslContext != null) {
Protocol protocol = new Protocol("https",
new SSLProtocolSocketFactory(sslContext), 443);
httpClient.getHostConfiguration().setHost(uri.getHost(),
uri.getPort(), protocol);
Protocol.registerProtocol("https", new Protocol("https",
new SSLProtocolSocketFactory(sslContext), 443));
}
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(params.getConnectTimeout());
@@ -169,7 +167,7 @@ public class HttpComponent3 extends AbstractHttpClient implements HttpClient {
}
private static class SSLProtocolSocketFactory implements
ProtocolSocketFactory {
SecureProtocolSocketFactory {
private final SSLContext sslContext;
@@ -207,5 +205,12 @@ public class HttpComponent3 extends AbstractHttpClient implements HttpClient {
UnknownHostException {
return sslContext.getSocketFactory().createSocket(host, port);
}
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host,
port, autoClose);
}
}
}
@@ -6,6 +6,7 @@ import java.io.InputStream;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import com.foxinmy.weixin4j.http.HttpClientException;
import com.foxinmy.weixin4j.http.HttpHeaders;
@@ -86,5 +87,6 @@ public class HttpComponent3Response implements HttpResponse {
@Override
public void close() {
httpMethod.releaseConnection();
Protocol.unregisterProtocol("https");
}
}
@@ -2,12 +2,18 @@ package com.foxinmy.weixin4j.http.factory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
@@ -17,6 +23,7 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.InputStreamEntity;
@@ -86,4 +93,33 @@ public abstract class HttpComponent4 extends AbstractHttpClient {
((HttpEntityEnclosingRequestBase) uriRequest).setEntity(httpEntity);
}
}
protected static class CustomHostnameVerifier implements
X509HostnameVerifier {
private final HostnameVerifier hostnameVerifier;
public CustomHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
}
@Override
public boolean verify(String hostname, SSLSession session) {
return hostnameVerifier.verify(hostname, session);
}
@Override
public void verify(String host, SSLSocket ssl) throws IOException {
}
@Override
public void verify(String host, X509Certificate cert)
throws SSLException {
}
@Override
public void verify(String host, String[] cns, String[] subjectAlts)
throws SSLException {
}
}
}
@@ -8,6 +8,7 @@ import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
@@ -71,7 +72,13 @@ public class HttpComponent4_1 extends HttpComponent4 implements HttpClient {
if (params.getSSLContext() != null) {
SSLSocketFactory socketFactory = new SSLSocketFactory(
params.getSSLContext());
Scheme scheme = new Scheme("https", socketFactory, 433);
X509HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
if (params.getHostnameVerifier() != null) {
hostnameVerifier = new CustomHostnameVerifier(
params.getHostnameVerifier());
}
socketFactory.setHostnameVerifier(hostnameVerifier);
Scheme scheme = new Scheme("https", socketFactory, 443);
httpClient.getConnectionManager().getSchemeRegistry()
.register(scheme);
}
@@ -8,6 +8,8 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
@@ -54,7 +56,13 @@ public class HttpComponent4_2 extends HttpComponent4 implements HttpClient {
requestConfig.setProxy(proxy);
}
if (params.getSSLContext() != null) {
X509HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
if (params.getHostnameVerifier() != null) {
hostnameVerifier = new CustomHostnameVerifier(
params.getHostnameVerifier());
}
httpClient = HttpClients.custom()
.setHostnameVerifier(hostnameVerifier)
.setSslcontext(params.getSSLContext()).build();
}
uriRequest.setConfig(requestConfig.build());
@@ -20,7 +20,6 @@ import com.foxinmy.weixin4j.http.entity.FormUrlEntity;
import com.foxinmy.weixin4j.http.entity.HttpEntity;
import com.foxinmy.weixin4j.http.entity.StringEntity;
import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
import com.foxinmy.weixin4j.http.factory.SimpleHttpClientFactory;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.WeixinErrorUtil;
@@ -45,7 +44,6 @@ public class WeixinRequestExecutor {
}
public WeixinRequestExecutor(HttpParams params) {
HttpClientFactory.setDefaultFactory(new SimpleHttpClientFactory());
this.httpClient = HttpClientFactory.getInstance();
this.params = params;
}
@@ -153,32 +151,30 @@ public class WeixinRequestExecutor {
}
protected void checkXml(WeixinResponse response) throws WeixinException {
XmlResult xmlResult = null;
try {
xmlResult = response.getAsXmlResult();
} catch (IllegalArgumentException ex) {
String xmlContent = response.getAsString();
if (xmlContent.length() != xmlContent.replaceFirst("<retcode>",
"<return_code>").length()) {
// <?xml><root><data..../data></root>
String newXml = response.getAsString()
.replaceFirst("<root>", "<xml>")
xmlContent = xmlContent.replaceFirst("<root>", "<xml>")
.replaceFirst("<retcode>", "<return_code>")
.replaceFirst("</retcode>", "</return_code>")
.replaceFirst("<retmsg>", "<return_msg>")
.replaceFirst("</retmsg>", "</return_msg>")
.replaceFirst("</root>", "</xml>");
xmlResult = XmlStream.fromXML(newXml, XmlResult.class);
response.setText(newXml);
}
XmlResult xmlResult = XmlStream.fromXML(xmlContent, XmlResult.class);
response.setText(xmlContent);
response.setXmlResult(true);
if (xmlResult.getReturnCode().equals("0")) {
if ("0".equals(xmlResult.getReturnCode())) {
return;
}
if (!xmlResult.getReturnCode().equalsIgnoreCase(
com.foxinmy.weixin4j.model.Consts.SUCCESS)) {
if (!com.foxinmy.weixin4j.model.Consts.SUCCESS
.equalsIgnoreCase(xmlResult.getReturnCode())) {
throw new WeixinException(xmlResult.getReturnCode(),
xmlResult.getReturnMsg());
}
if (!xmlResult.getResultCode().equalsIgnoreCase(
com.foxinmy.weixin4j.model.Consts.SUCCESS)) {
if (!com.foxinmy.weixin4j.model.Consts.SUCCESS
.equalsIgnoreCase(xmlResult.getResultCode())) {
throw new WeixinException(xmlResult.getErrCode(),
xmlResult.getErrCodeDes());
}
@@ -42,7 +42,6 @@ public class WeixinResponse {
try {
text = StringUtil.newStringUtf8(IOUtil.toByteArray(body));
} catch (IOException e) {
e.printStackTrace();
;
}
}
@@ -1,9 +1,7 @@
package com.foxinmy.weixin4j.http.weixin;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;