gpt4 book ai didi

java.net.SocketException : Software caused connection abort: recv failed when using different jre security 异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:02 25 4
gpt4 key购买 nike

我在尝试使用在同一网络中的主机上运行的 HttpsURLConnection 连接到 Web 服务时遇到以下异常。防火墙已关闭。有趣的是,如果我在 Eclipse 中通过默认的 Java JRE 1.7 环境运行以下代码,一切正常。

但是如果我在有自己的 jre 文件夹的产品代码中使用这些函数,它会抛出以下异常 -

Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:173)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at com.rsa.sslj.x.aP.c(Unknown Source)
at com.rsa.sslj.x.aP.a(Unknown Source)
at com.rsa.sslj.x.aP.a(Unknown Source)
at com.rsa.sslj.x.aP.h(Unknown Source)
at com.rsa.sslj.x.cy.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

我注意到 java.security 文件中的安全提供者列表不同。

这是列表 -

security.provider.1=com.rsa.jsafe.provider.JsafeJCE
security.provider.2=com.rsa.jsse.JsseProvider
security.provider.3=sun.security.provider.Sun
security.provider.4=sun.security.rsa.SunRsaSign
security.provider.5=com.sun.net.ssl.internal.ssl.Provider
security.provider.6=com.sun.crypto.provider.SunJCE
security.provider.7=sun.security.jgss.SunProvider
security.provider.8=com.sun.security.sasl.Provider
security.provider.9=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.10=sun.security.smartcardio.SunPCSC
security.provider.11=sun.security.mscapi.SunMSCAPI

这些是我的功能-

private InputStream Get(URL url,String keyStoreString, String keyStorePassword) {
SSLSocketFactory sslFactory;
try {
sslFactory = getSSLSocketFactory(keyStoreString, keyStorePassword);
javax.net.ssl.HttpsURLConnection
.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
if (hostname.equals("vmm")) {
return true;
}
return false;
}
});
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(sslFactory);
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
InputStream responseStream = con.getInputStream();
return responseStream;
}catch (Exception e) {
//log errer
}
}
private SSLSocketFactory getSSLSocketFactory(String keyStoreString,
String password) throws KeyStoreException,
NoSuchAlgorithmException, CertificateException, IOException,
UnrecoverableKeyException, KeyManagementException, AzureException {
KeyStore ks = getKeyStore(keyStoreString, password);
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance("SunX509");
keyManagerFactory.init(ks, password.toCharArray());
// Trustmanager which trusts all certificate. Not a good idea in
// production code.
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(final X509Certificate[] chain,
final String authType) {
}

@Override
public void checkServerTrusted(final X509Certificate[] chain,
final String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };

SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), trustAllCerts,
new SecureRandom());

return context.getSocketFactory();
}

我需要让这些功能在产品代码中发挥作用。任何帮助将不胜感激。提前致谢!!

最佳答案

当 RSA Jsafe 的优先级高于 Sun 提供程序时,尝试使用 SunX509 KeyManagerFactory 可能会导致与幕后其他工厂的混淆。

如果 RSA Jsafe 提供了自己的 KeyManagerFactory 实现,也许可以尝试:

KeyManagerFactory keyManagerFactory = 
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

(除非您真的想要 SunX509 KMF,否则在您的事业中使用默认算法通常是更好的选择,这样就不必依赖于特定的提供商。适用时,对于 TrustManagerFactories 也是如此。)

关于java.net.SocketException : Software caused connection abort: recv failed when using different jre security 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22760124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com