【发布时间】:2015-08-29 23:31:19
【问题描述】:
这段代码有什么问题,它应该信任所有主机,但它没有..
它适用于例如 google.com,但不适用于在我的机器上本地运行的 API 网关服务,为什么?
SSL 调试输出
触发 SecureRandom 播种完成播种 SecureRandom Ignoring 不支持的密码套件:TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 ... 忽略不支持的密码套件:TLS_RSA_WITH_AES_128_CBC_SHA256 允许不安全的重新协商:false 允许遗留的 hello 消息:true 是 初始握手:true 是否安全重新协商:false Thread-6, setSoTimeout(0) 调用 %% 没有缓存的客户端会话 *** ClientHello,TLSv1 RandomCookie:GMT:1434280256 字节 = { 216 ... 40 } 会话 ID:{} 密码套件: [TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, .... SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA、SSL_RSA_WITH_RC4_128_MD5、 TLS_EMPTY_RENEGOTIATION_INFO_SCSV] 压缩方法:{ 0 } 扩展椭圆曲线,曲线名称:{secp256r1 .. secp256k1} 扩展 ec_point_formats,格式:[未压缩]
线程 6,写入:TLSv1 握手,长度 = 163 线程 6,读取:TLSv1 警报,长度 = 2 Thread-6,RECV TLSv1 ALERT:致命, handshake_failure 线程 6,调用 closeSocket() 线程 6,处理 异常:javax.net.ssl.SSLHandshakeException:**
收到致命警报:handshake_failure
**
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
public class ConnectHttps {
public static void main(String[] args) throws Exception {
/*
* fix for
* Exception in thread "main" javax.net.ssl.SSLHandshakeException:
* sun.security.validator.ValidatorException:
* PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
* unable to find valid certification path to requested target
*/
TrustManager[] trustAllCerts = [
[ getAcceptedIssuers: { -> null },
checkClientTrusted: { X509Certificate[] certs, String authType -> },
checkServerTrusted: { X509Certificate[] certs, String authType -> } ] as X509TrustManager
]
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// 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
*/
//URL url = new URL("https://google.com"); //WORKS
URL url = new URL("https://localhost:8090"); // DOES NOT WORK, WHY?
URLConnection con = url.openConnection();
Reader reader = new InputStreamReader(con.getInputStream());
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
运行the code found here表明客户端没有启用TLSv1.2:
支持的协议:5
SSLv2Hello
SSLv3
TLSv1
TLSv1.1
TLSv1.2启用的协议:2
SSLv3
TLSv1
【问题讨论】:
标签: java ssl groovy certificate