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

发布于 2021-12-04 23:35:24 字数 4761 浏览 984 评论 11

各位大牛好:

      我在用程序调用微信公众平台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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

彼岸花ソ最美的依靠 2021-12-06 16:25:16

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

1不用openJDK 使用sun的JDK

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

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

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

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

凌乱心跳 2021-12-06 16:25:16

我之前是因为把密钥有人给改了,我改成最新的密钥之后可以用了。。。

倾城泪 2021-12-06 16:25:15

 yum upgrade nss 然后重启服务器试试,或者实例

白龙吟 2021-12-06 16:25:13

楼主,此问题解决了吗

因为看清所以看轻 2021-12-06 16:25:13

楼主,这个问题解决啦吗?我换了jdk还是有问题

平定天下 2021-12-06 16:24:54

请问下改tomcat配置里面的什么,能说的具体些吗?谢谢了

命硬 2021-12-06 15:56:57

我也遇到了,这个怎么解决,各位大神们。

少女情怀诗 2021-12-06 14:02:11

能详细说说么,我也遇到了类似的情况

多彩岁月 2021-12-06 10:03:34

换了就好了!

别低头,皇冠会掉 2021-12-06 08:18:00

麻烦详细点,刚遇到了这个问题,不知道怎么解决

醉酒的小男人 2021-12-05 10:38:40

换成sun的jdk问题搞定。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文