0

I have a requirement from a server application to share a SSL certificate. step i did:

  1. I generated a self signed certificate against the Domain IP address(don't have domain name) where my application is deployed.
  2. i shared the certificate to the server. they will keep the certificate in their trust store.
  3. server is validating the request for the IP address. if the request are not coming from the IP address they are stopping them.

My question:

i have a spring boot application. do i need to make any change in my code for the certificate i have generated. if yes then what is the change.

James Z
  • 12,209
  • 10
  • 24
  • 44
Shilpi
  • 109
  • 3
  • 12

1 Answers1

0

Yes you need to make changes in your code. You need to load your keystore(with keypair) and if required also load your truststore into your http client. Most of the http clients require a SSLContext, so this would be sufficient for you:

KeyStore keyStore = ...;
TrustStore trustStore = ...;

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, keyStorePassword);

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);

SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

// Spring provides by default RestTemplate as HTTP Client, this client is an Apache HTTP Client wrapper
// The setup would be:

HttpClient httpClient = HttpClient.newBuilder();
        .sslContext(sslFactory.getSslContext());
        .build();

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory)
Hakan54
  • 3,121
  • 1
  • 23
  • 37