Skip to content

Instantly share code, notes, and snippets.

@juddflamm
Last active July 13, 2018 01:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juddflamm/5391938 to your computer and use it in GitHub Desktop.
Save juddflamm/5391938 to your computer and use it in GitHub Desktop.
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