0

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();
  • `server.ssl` settings are for handling HTTPS connections and requests _TO_ your app from _other_ programs like browsers; they have no effect on connections you create. For RestTemplate try https://stackoverflow.com/questions/75783226/using-resttemplate-for-making-a-https-call-with-certificates-and-keys-in-spring or maybe https://stackoverflow.com/questions/42323468/how-to-call-https-restful-web-services-using-spring-resttemplate – dave_thompson_085 Jun 21 '23 at 19:34

0 Answers0