调用微信公众https接口出现KeyException异常

雨佳 发布于 2015/09/26 02:09
阅读 5K+
收藏 1

各位大牛好:

      我在用程序调用微信公众平台https的时候,本地测试调用方法是可以获取到信息的,但是将程序放在服务器上,就出现以下问题:

javax.net.ssl.SSLException: java.security.ProviderException: java.security.KeyException
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208) ~[na:1.7.0_85]
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1916) ~[na:1.7.0_85]
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1874) ~[na:1.7.0_85]
at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1857) ~[na:1.7.0_85]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1378) ~[na:1.7.0_85]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355) ~[na:1.7.0_85]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) ~[httpclient-4.3.5.jar:4.3.5]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) ~[httpclient-4.3.5.jar:4.3.5]


我已经尝试过网上很多忽略ssl的方法来测试,都没有成功,各位大牛应该怎么解决啊,谢谢啦!

public static String get(String url) throws IOException {
StringBuffer buffer = new StringBuffer();
try {
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL _url = new URL(url);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) _url
.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod("GET");
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String content = null;
while ((content = bufferedReader.readLine()) != null) {
buffer.append(content);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}


public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}


class AnyTrustStrategy implements TrustStrategy {


@Override
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}


}

以上方法都测试过,没有效果。

服务器的配置是: centos + tomcat + mysql + openJDK

加载中
1
电脑小童
电脑小童
换成sun的jdk问题搞定。
三百块丶会走路
麻烦详细点,刚遇到了这个问题,不知道怎么解决
l
leafisme
换了就好了!
terrence_tian
terrence_tian
老子也遇到这样的问题了,这腾讯也TM太坑了。
贾俊俊
能详细说说么,我也遇到了类似的情况
0
小刺客
小刺客
我也遇到了,这个怎么解决,各位大神们。
0
一个往回走的旅客
一个往回走的旅客
服务器本身是否支持https的服务?tomct似乎需要改配置,变成支持https请求的。可以将微信回调的自己的地址改成http的,不使用https,不影响开发。
三百块丶会走路
请问下改tomcat配置里面的什么,能说的具体些吗?谢谢了
0
m
miou

楼主,此问题解决了吗

0
神是到着念
神是到着念
楼主,这个问题解决啦吗?我换了jdk还是有问题
0
a
andrew_123
 yum upgrade nss 然后重启服务器试试,或者实例
0
fhj11032_806089
fhj11032_806089
yum upgrade nss 然后重启tomcat试试
0
颖辉小居
颖辉小居

找到两种方法(请通过链接打开查看详情):

1不用openJDK 使用sun的JDK

http://blog.csdn.net/luwei42768/article/details/51194197

2 更新nss(推荐 我就是通过这种方式解决的)

http://blog.csdn.net/zixiao217/article/details/53438530

本人因为是正式环境不敢轻易尝试,大家如果解决了请回复告知。

0
小刺客
小刺客
我之前是因为把密钥有人给改了,我改成最新的密钥之后可以用了。。。
返回顶部
顶部