We had to use certs through the code to make API calls to certain applications.
We could not install the certs on the code container for some reasons. The API service provider gave us the .cer file.Apache HTTP client was used for this purpose
Start with first creating a file based keystore and loaded this .cer file onto it
keytool -import -alias joe -file <path>/file.cer -keystore <keystoreName> -storepass <password>
Then add the generated keystore file as a resource into the application
and build your custom HTTPClient to use this keystore
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import java.io.File;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
File file = new File(getClass().getClassLoader().getResource(<pathTokeystoreFile>).getFile());
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(file, CERT_PASSWORD.toCharArray(),
new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1", "SSLv3", "TLSv1.1", "TLSv1.2"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient customClient;
customClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
}