使用HttpClient进行相互身份验证

时间:2012-07-27 10:42:38

标签: https apache-httpclient-4.x

我将提出我的问题:

我的Web应用程序通过https将表单提交给服务器,该服务器需要客户端证书才能设置通信。它工作正常,当我提交表单时,https服务器要求我提供证书,我选择我的证书和服务器响应我。但现在,我想从一个带有HttpClient的servlet发布到https服务器,因为我想管理来自https服务器的响应,这里我有问题......

使用HttpClient发送帖子的代码是:

DefaultHttpClient client = new DefaultHttpClient();

// in the truststore i have the https server certificates for establish connection
// from my client to https server       
FileInputStream instream = new FileInputStream(new File(TRUESTORE_PATH));       
KeyStore truststore  = KeyStore.getInstance(KeyStore.getDefaultType());
truststore.load(instream, KEY_TRUESTORE_PASS.toCharArray());

// in the keystore i have the client certificate but without private key because,
// in theory, i shouldn't know it   
FileInputStream otherinstream = new FileInputStream(new File(KEYSTORE_PATH));
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(otherinstream,KEY_STORE_PASS.toCharArray());

SSLSocketFactory socketFactory = new SocketFactory(keystore,KEY_STORE_PASS,truststore);
Scheme sch = new Scheme("https", 443, socketFactory);
client.getConnectionManager().getSchemeRegistry().register(sch);

HttpGet httpget = new HttpGet("https://remote_server_with_client_authentication");
HttpResponse response = client.execute(httpget);

当我执行此代码时,例如在单元测试中,我有来自https服务器的响应,但它告诉我未授权。我怀疑我无法通过https服务器进行身份验证,因为密钥库具有客户端证书但是whitout私钥,然后我没有被授权。

然后,有没有办法与用户选择的客户端证书建立与https服务器的通信?

谢谢!

0 个答案:

没有答案