0

I am trying to create a RestTemplate in Spring Boot (java) that connects to an API endpoint using TLS. The catch is that I have to use a public key that I get from a custom API and have saved into a String variable.

I think I need to do something like this:

public RestTemplate getSSLRestTemplate() throws NoSuchAlgorithmException, KeyStoreException {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        /** What do I do here? **/
        SSLContext sslContext = SSLContext.getInstance("TLS");
        
        SSLConnectionSocketFactory sslConFactory = new SSLConnectionSocketFactory(sslContext);

        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConFactory).build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        return new RestTemplate(requestFactory);
}

But, I'm not quite sure how to inject my string into a KeyStore and pass it to the SSLContext.

The public key is in the form:

-----BEGIN CERTIFICATE-----
.......
.......
-----END CERTIFICATE-----

How do I pass my publickey String into the RestTemplate's SSLContext?

Karthik Sankaran
  • 217
  • 2
  • 11

0 Answers0