【发布时间】:2010-10-26 21:41:30
【问题描述】:
我正在使用 Java 6,并尝试使用客户端证书针对远程服务器创建 HttpsURLConnection。
服务器正在使用自签名根证书,并要求提供受密码保护的客户端证书。我已将服务器根证书和客户端证书添加到我在/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/lib/security/cacerts (OSX 10.5) 中找到的默认 java 密钥库中。
密钥库文件的名称似乎表明不应将客户端证书放入其中?
不管怎样,把根证书加到这个商店里解决了臭名昭著的javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed' problem.
但是,我现在不知道如何使用客户端证书。我尝试了两种方法,但都没有成功。
首先,也是首选,尝试:
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = new URL("https://somehost.dk:3049");
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(sslsocketfactory);
InputStream inputstream = conn.getInputStream();
// The last line fails, and gives:
// javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
我尝试跳过 HttpsURLConnection 类(不理想,因为我想与服务器通信 HTTP),而是这样做:
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("somehost.dk", 3049);
InputStream inputstream = sslsocket.getInputStream();
// do anything with the inputstream results in:
// java.net.SocketTimeoutException: Read timed out
我什至不确定客户端证书是这里的问题。
【问题讨论】:
-
我已经从客户端提供了两个证书,如何确定需要在密钥库和信任库中添加哪个证书,您能否帮助确定这个问题,因为您已经经历过类似的问题stackoverflow.com/questions/61374276/…
标签: java ssl jsse sslhandshakeexception