java http client with certificate(.crt) and private key(.pem) with a passphrase

Viewed 752

I'm new to java and find it very difficult for HTTP communication. I'm trying to convert the below curl request in java which uses a .crt certificate along with a pem private key.

curl -X POST 'https://oauthasservices.cert.mydomain.com/oauth2/apitoken?grant_type=client_credentials'\
-H 'Accept: application/json' \
--cert my_cert.crt \
--key my-private-key.pem \
-d 'grant_type=client_credentials'
Enter PEM pass phrase: myphrase ==> access token retrieved .

I'm not able to understand how can I pass the private key and passphrase while creating SSL context/ key Store in Java.

    keyStore.load(new FileInputStream(keyStorePath), keystorePassword.toCharArray());
    KeyManagerFactory kmf = 
    KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, keystorePassword.toCharArray());
    SSLContext ctx = SSLContext.getInstance("TLS");// something else?
    ctx.init(kmf.getKeyManagers(), null, null);
    SSLSocketFactory sslSocketFactory = ctx.getSocketFactory();

Please let me know What is the best way to deal with crt cert file along with private key?

0 Answers
Related