Having Separate Certificates Running Under GlassFish 2

116 Views Asked by At

Can anyone please explain how i can have more than one X.509 Certificates in my GlassFish application server? The main challenge for me is that GlassFish uses just one alias which is 's1as'.

1

There are 1 best solutions below

0
TheArchitect On BEST ANSWER

You can pull additional certificates from external key files to create an SSLContext and then SSLSocketFactory, which you can feed into your external HTTPS calls.

E.g.:

KeyStore cKeyStore = KeyStore.getInstance("PKCS12");
try (InputStream clientCertKeyInput = new FileInputStream("my.pfx")) {
     cKeyStore.load(clientCertKeyInput, "password".toCharArray());
}
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(cKeyStore, "password".toCharArray());

SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(keyManagerFactory.getKeyManagers(), 
            null, // default javax.net.ssl.trustStore
            new SecureRandom()); 

SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();

You may then configure an HttpsURLConnection with it:

httpsConn.setSSLSocketFactory(sslSocketFactory);

Or if you're using JAXWS set it as a property of the BindingProvider's context:

Map<String, Object> ctxt = ((BindingProvider) port).getRequestContext();
ctxt.put(JAXWSProperties.SSL_SOCKET_FACTORY, sslSocketFactory);

Hope this helps.