I have a spring boot application that is running on my server. I need to make a POST api call to an external api that is in another server. Now I need to make a secure connection. I have our own certificate (CA signed) and their certificate(they did not provide me any private key). Both in PEM Format. I have converted my certificate file sample.crt using my private key to pkcs12 format.
sudo openssl pkcs12 -export -in sample.crt -inkey private.key -out
sample.p12 -name sample
Then I have converted their certificate to jks format and import it to truststore using keytool
sudo keytool -import -file thirdparty.crt -alias thirdparty -keystore
truststore.jks
I store these files to src/main/resources
my application.properties file is:
server.port=8332
server.ssl.key-store=classpath:sample.p12
server.ssl.key-store-password=password
server.ssl.key-alias=sample
server.ssl.key-store-type=PKCS12
server.ssl.trust-store=classpath:truststore.jks
server.ssl.trust-store-password=password
server.ssl.trust-store-type=JKS
my api call classs:
headers.setContentType(MediaType.APPLICATION_JSON);
String requestBody = "{"sample request body"}";
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
ResponseEntity<String> response = restTemplate.exchange(
"apiURL",
HttpMethod.POST,
entity,
String.class
);
String responseBody = response.getBody();