【问题标题】:How to use SSL Client certificate with Apache commons http client如何将 SSL 客户端证书与 Apache commons http 客户端一起使用
【发布时间】: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


【解决方案1】:

我们在后台服务部分使用的*.jks。

我可以给你一个我的项目 Java Spring boot 的例子,我在我的后台服务中更改了 http --> https 并在 Nginx 中添加了我的证书。

Example of https simple services

当您更改回服务时,您可以直接在您的前端应用程序中调用 https(例如 web 角度)。

【讨论】:

  • 我已经配置了 Spring Boot 服务,但是客户端应用程序正在使用 commons-httpclient 库来调用该服务。我已经更新了客户端代码。
  • @SarveshH 我认为您应该首先使用邮递员测试您的 https API,以确保您的 API https 正常工作。你可以按照这个来打电话,看起来你想做的事情prasans.info/making-https-call-using-apache-httpclient我更喜欢这个解决方案:mkyong.com/java/apache-httpclient-examples但他不使用jks
  • @SarveshH Commons HttpClient 项目现已终止,不再开发。它已被其 HttpClient 和 HttpCore 模块中的 Apache HttpComponents 项目所取代,这些模块提供了更好的性能和更大的灵活性。 hc.apache.org/httpclient-legacy
【解决方案2】:

我使用了以下对我有用的实现,因为限制不升级 http 客户端库。

System.setProperty(JAVAX_NET_SSL_TRUSTSTORE, "H://certificateFile.jks");
System.setProperty(JAVAX_NET_SSL_TRUSTSTORE_KEY, "abcd");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2020-01-23
    • 1970-01-01
    • 2022-07-01
    • 2013-06-20
    • 2011-03-04
    • 2010-12-24
    相关资源
    最近更新 更多