Kalhara Amarasinghe
Kalhara Amarasinghe

Reputation: 73

Using .p12 file for SSL - Using Spring Boot

I want to connect to a secure server endpoint. The service provider has provided me with a .p12 file and the password. I tried the API call on postman after adding the certificate under settings --> certificates--> client certificates and it was a success.

Now I want to integrate this to my java code. I am developing this application using Spring boot and I followed a couple of guides for this.I added the the .p12 file to my keystore using this command.

keytool -importkeystore -srckeystore uat_client.p12 -destkeystore store.keys -srcstoretype pkcs12 -alias myownkeyname 

But every time I get "No SSL Client Certificate Provided"which is the same response I got when I did not add the certificate in the postman.

I know this is not the ideal way to ask help since I do not have much code to show.And I am very new to this area of java. I am merely asking for some guide. If I have a .p12 file what are the steps I need to follow to connect to the service successfull.

public void someMethod()
{
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders httpHeaders = new HttpHeaders();

    String uri = "https://xx.net/api/v2/somemethod";

    httpHeaders.setContentType(MediaType.APPLICATION_JSON);

    String result = restTemplate.getForObject(uri, String.class);

    System.out.println("Result " + result);
}

Upvotes: 7

Views: 15918

Answers (2)

Imran
Imran

Reputation: 1902

Nothing works, simply throw this feign thing away and use rest template. It worked from the first attempt after spending an entire day on feigns bugs.

Upvotes: 0

fatih
fatih

Reputation: 1420

You must configure your RestTemplate to use the client certificate in an HTTPS connection as follows.

@Bean
public RestTemplate restTemplate() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException {
    KeyStore clientStore = KeyStore.getInstance("PKCS12");
    clientStore.load(new FileInputStream("certfile-path"), "certificate-password".toCharArray());
 
    SSLContext sslContext = SSLContextBuilder.create()
            .setProtocol("TLS")
            .loadKeyMaterial(clientStore, "certificate-password".toCharArray())
            .loadTrustMaterial(new TrustSelfSignedStrategy())
            .build();
 
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext);
    CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(sslConnectionSocketFactory)
            .build();

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

    return new RestTemplate(requestFactory);
}

and Inject restTemplate for using:

@Autowired
RestTemplate restTemplate;

public void someMethod() {
    HttpHeaders httpHeaders = new HttpHeaders();
    String uri = "https://xx.net/api/v2/somemethod";
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    String result = restTemplate.getForObject(uri, String.class);
    System.out.println("Result " + result);
}

Upvotes: 13

Related Questions