1

我要提出我的问题:

我的 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 服务器进行身份验证,因为密钥库具有客户端证书但没有私钥,那么我没有被授权。

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

谢谢!

4

0 回答 0