我正在使用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
Run Code Online (Sandbox Code Playgroud)
我试过跳过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
Run Code Online (Sandbox Code Playgroud)
我甚至不确定客户端证书是否存在问题.