Skip to content

Instantly share code, notes, and snippets.

@mrserverless
Last active February 17, 2017 09:46
Show Gist options
  • Save mrserverless/7687f67a8eeade0c92d5 to your computer and use it in GitHub Desktop.
Save mrserverless/7687f67a8eeade0c92d5 to your computer and use it in GitHub Desktop.
Dropwizard JerseyClient loading JKS certificate into SSL TrustStore at runtime
private TrustManager[] loadTrustManagers(String certificateFile) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
KeyStore trustedCertStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustedCertStore.load(SecurityModule.class.getClassLoader().getResourceAsStream(), null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustedCertStore);
return tmf.getTrustManagers();
}
private Registry<ConnectionSocketFactory> createSSLSocketRegistry(TrustManager[] trustManagers) throws NoSuchAlgorithmException, KeyManagementException {
final SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
ctx.init(null, trustManagers, null);
return RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", new SSLConnectionSocketFactory(ctx))
.build();
}
public JerseyClient createSSLJerseyClient (Environment environment, JerseyClientConfiguration clientConfiguration) throws Exception {
final String CERT_FILE = "jssecacerts.jks";
TrustManager[] trustManager = loadTrustManagers(CERT_FILE);
Registry<ConnectionSocketFactory> = createSSLSocketRegistry(trustManager);
return new JerseyClientBuilder(environment)
.using(clientConfiguration)
.using(sslSocketRegistry)
.build("SSLJerseyClient");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment