Last active
February 17, 2017 09:46
-
-
Save mrserverless/7687f67a8eeade0c92d5 to your computer and use it in GitHub Desktop.
Dropwizard JerseyClient loading JKS certificate into SSL TrustStore at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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