简体   繁体   English

如何将 SSL 客户端证书与 Apache commons http 客户端一起使用

[英]How to use SSL Client certificate with Apache commons http client

My application is using Apache Commons HTTP Client to consume HTTP service URL.我的应用程序使用 Apache Commons HTTP 客户端来使用 HTTP 服务 URL。 Now we have to move over HTTPS endpoint URL.现在我们必须移动 HTTPS 端点 URL。 To consume the same, we received SSL Client Certificate.为了使用相同的内容,我们收到了 SSL 客户端证书。 How we can use .JKS with password while consuming HTTPS URL ?我们如何在使用 HTTPS URL 时使用带密码的 .JKS ? (Due to application limitations cant use other APIs) (由于应用程序限制不能使用其他 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);

The *.jks we use in the back service part.我们在后台服务部分使用的 *.jks。

I can give you a example of my project Java Spring boot, I change http --> https in my back service and I added my certificate in Nginx.我可以给你一个我的项目 Java Spring Boot 的例子,我在我的后台服务中更改了 http --> https,并在 Nginx 中添加了我的证书。

Example of https simple services https 简单服务示例

When you changed back service you can call https directly in your front application(ex.web angular).当您改回服务时,您可以直接在您的前端应用程序(例如 web angular)中调用 https。

I used below implementation which worked for me as had limitation not to upgrade the http client libraries.我使用了以下对我有用的实现,因为有不升级 http 客户端库的限制。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM