Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Enabling 2 Way SSL Client Service Calls from within Dropwizard. To do so, you need to load your keystore and truststore and configure HttpClient to us them for HTTPS calls. In this case, my keystore and truststore are the same file with the same password. (Thanks to Coda Hale for an initial solution)
//First create the httpClient in Dropwizard's run method as documented
final HttpClient httpClient = new HttpClientBuilder().using(configuration.getHttpClient()).build();
try {
//Create KeyStore obejcts for both the keystore and truststore
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
//Then load the actual keystore/truststore file(s), they are the same file in my case
keystore.load(new FileInputStream(configuration.getKeyStore()), configuration.getKeyStorePassword().toCharArray());
truststore.load(new FileInputStream(configuration.getKeyStore()), configuration.getKeyStorePassword().toCharArray());
//Then register a Scheme for HTTPS, in the httpClient, using your loaded keystore, keyPassword, and truststore
//The keypassword, second argument, is the password of your key, not the keystore.
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443,
new SSLSocketFactory(keystore, configuration.getKeyStorePassword(), truststore)));
} catch (Throwable t) {
//If something goes wrong, just kill Dropwizard with a RuntimeException
throw new RuntimeException("Couldn't register the HTTPS scheme in HttpClient", t);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment