【发布时间】:2021-07-26 18:52:00
【问题描述】:
我的应用程序正在使用 Apache Commons HTTP 客户端来使用 HTTP 服务 URL。现在我们必须移动 HTTPS 端点 URL。为了使用它,我们收到了 SSL 客户端证书。我们如何在使用 HTTPS URL 时使用 .JKS 和密码? (由于应用限制不能使用其他API)
KeyStore identityKeyStore = KeyStore.getInstance("JKS");
FileInputStream identityKeyStoreFile = new FileInputStream(new File(certificatePath));
identityKeyStore.load(identityKeyStoreFile, password.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(identityKeyStore);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(identityKeyStore, password.toCharArray());
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);
SSLContext.setDefault(sslContext);
PostMethod post = new PostMethod("https://url");
HttpClient httpClient = new HttpClient();
String reqMessage = getSolaceRequestMessage(message,hostName,port,authentication);
Part[] parts = {
new StringPart("reqMessage", message),
};
post.setRequestEntity(
new MultipartRequestEntity(parts, post.getParams())
);
httpClient.executeMethod(post);
【问题讨论】:
-
@YiaoSUN 感谢您的回复。我的应用程序中使用了以下库。您共享的上述链接正在使用 http 组件客户端库。
<groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> -
@stdunbar 不,我的应用程序使用
commons-httpclient库。上面的链接显示了 apache 组件。 -
你是对的@SarveshH - 你正在使用一个 8 年没有更新并且不再受支持的库。我撤回了我的近距离投票。
标签: java apache-commons-httpclient