Reputation: 85
I was hoping if I could get some help regarding an issue I have been facing today:
I am trying to authenticate my client with server of one of our clients, I am able to do so by issuing the following command:
curl -v -k -H "Content-Type:application/json" --key privkey.pem --cert pub.cer --data @search.json https://....
As you can tell from the command above I have the following:
keystore
Now, I am trying to do the same thing in java, but I have no clue how to get it done. All the guides that I have read tell me that I should use the keystore I have. But I hit roadblocks when following those guides.
I anybody could help or point me to a certain direction, I would greatly appreciate it.
Thanks in advance, Peter
Upvotes: 4
Views: 8948
Reputation: 8928
It sounds like you need to use an HTTPS URL connection to connect with a server which requires client authentication. You'll need to do two things to get there from where you are.
First, you'll need to create a Java style keystore from your private key and public certificate. Detailed instructions can be found in the answers to this question:
importing an existing x509 certificate and private key in Java keystore to use in ssl
You'll also need to import the server's certificate into the keystore.
Second, you'll need to write your Java code to use your newly created keystore. Do this by creating an SSLContext using your keystore, and setting your HTTPS URL connection to use a socket factory from this context - something along the lines of this:
SSLContext sslContext
= SSLConnections.getSSLContext(keyStoreFile, keyStoreFilePassword);
httpsURLConnection.setSSLSocketFactory(sslContext.getSocketFactory());
This should be done after the HttpsURLConnection is created, but before it is connected - that is, before you read from or write to it, or call connect() on it.
Upvotes: 2