简体   繁体   中英

Establishing connection to HTTPS using client certificates

I want to upload a file to a HTTPS-server using Java. The server is not a open URL hence I need a client certificates to establish a connection. I am having .pem , .jks , .pkcs12 client certificate files.

Can any one suggest me how to use this certificate files in my application to establish communication ? Do I need to use all 3 certificate files ?

The .jks file is the Java Keystore. It should contain the correct client certificates (and maybe also the intermediate certificates from the certificate chain).

I assume you are going to write a client that uploads the file to the HTTPS server? Then you should use the .jks file with the client certificate with the (let's say apache) HttpClient .

You need to create a SSLContext and load the keystore

SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File("keystore", "yourPassword".toCharArray(), new TrustSelfSignedStrategy()).build();

Then you have to put the sslContext in a SSLConnectionSocketFactory

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

And then finally build the HttpClient

HttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

After all these steps the httpClient should use your client certificate from the keystore for your desired request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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