javax.net.ssl.SSLException:收到致命警报:bad_record_mac [英] javax.net.ssl.SSLException: Received fatal alert: bad_record_mac
问题描述
我正在获取https连接的javax.net.ssl.SSLException:收到致命警报:bad_record_mac。每次请求都不会发生这种情况 - 如果我在10次内发送相同的请求,我只会收到一次或两次此错误。
I'm getting a "javax.net.ssl.SSLException: Received fatal alert: bad_record_mac" for an https connection. This doesn't happen for every request -- if I send the same request in 10 times I only get this error once or twice.
我有以下代码来验证证书:
I have the following code to validate the certificate:
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSLv3");
} catch (NoSuchAlgorithmException e3) {
logException(Arrays.toString(e3.getStackTrace()));
}
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory factory = sslContext.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(factory);
} catch (KeyManagementException e) {
logException(Arrays.toString(e.getStackTrace()));
}
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
/*
* end of the fix
*/
我在main方法中设置了两个系统属性:
I've set two system properties in my main method:
System.setProperty("jsse.enableSNIExtension", "false");
System.setProperty("https.protocols", "SSLv3");
但没有任何帮助。
请帮助!
谢谢。
推荐答案
这很难说是什么造成的。你需要通过分析日志来找出,设置
It is a hard to say what causing this. You need to find out by analyzing the logs, set the
System.setProperty("javax.net.debug", "all");
并检查有什么问题。
问题可能是服务器不支持TLS,可能会被实现选择。要确保始终通过设置属性来使用普通SSLv3:
A problem may be that server is not supporting TLS, which may be picked by implementation. To make sure that you always use the plain SSLv3 by setting the property:
System.setProperty("https.protocols", "SSLv3");
另一个原因可能是网络状况不佳,偶尔导致某些数据包被破坏,但仍然有正确的校验和。在这种情况下,您只能重试请求。
Another cause may be the poor network, causing that occasionally some packets are broken but still have the correct checksum. In that case you may only retry the request.
这篇关于javax.net.ssl.SSLException:收到致命警报:bad_record_mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!