SecurityUtils { public static SSLContext createSSLContext(Map<String, ?> sslConfigurations, String sslAlgorithm) throws Exception { SSLContext context = SSLContext.getInstance(sslAlgorithm); KeyManager[] km = null; String keyStorePath = (String)sslConfigurations.get("keyStorePath"); if (keyStorePath == null || keyStorePath.isEmpty()) { keyStorePath = System.getProperty("javax.net.ssl.keyStore"); } String keyStorePassword = (String)sslConfigurations.get("keyStorePassword"); if (keyStorePassword == null || keyStorePath.isEmpty()) { keyStorePassword = Optional.ofNullable(System.getProperty("javax.net.ssl.keyStorePassword")).orElse(""); } String keyStoreType = (String)sslConfigurations.get("keyStoreType"); if (keyStoreType == null || keyStoreType.isEmpty()) { keyStoreType = System.getProperty("javax.net.ssl.keyStoreType"); } String trustStorePath = (String)sslConfigurations.get("trustStorePath"); if (trustStorePath == null || trustStorePath.isEmpty()) { trustStorePath = System.getProperty("javax.net.ssl.trustStore"); } String trustStorePassword = (String)sslConfigurations.get("trustStorePassword"); if (trustStorePassword == null || trustStorePassword.isEmpty()) { trustStorePassword = Optional.ofNullable(System.getProperty("javax.net.ssl.trustStorePassword")).orElse(""); } String trustStoreType = (String)sslConfigurations.get("trustStoreType"); if (trustStoreType == null || trustStoreType.isEmpty()) { trustStoreType = System.getProperty("javax.net.ssl.trustStoreType"); } Object obj = sslConfigurations.get("serverCertValidation"); boolean serverCertValidation = (obj == null) || Boolean.parseBoolean(obj.toString()); if (keyStorePath != null) { KeyStore ks = KeyStore.getInstance(keyStoreType != null ? keyStoreType : KeyStore.getDefaultType()); InputStream in = getFileInputStream(keyStorePath); try { ks.load(in, keyStorePassword.toCharArray()); } finally { if (in != null) { in.close(); } } KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, keyStorePassword.toCharArray()); km = kmf.getKeyManagers(); } TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); TrustManager[] tm = null; if (serverCertValidation) { if (trustStorePath != null) { KeyStore trustStore = KeyStore.getInstance(trustStoreType != null ? trustStoreType : KeyStore.getDefaultType()); InputStream in = getFileInputStream(trustStorePath); try { trustStore.load(in, trustStorePassword.toCharArray()); trustManagerFactory.init(trustStore); tm = trustManagerFactory.getTrustManagers(); } finally { if (in != null) { in.close(); } } } } else { TrustManager ignoreValidationTM = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }; tm = new TrustManager[] {ignoreValidationTM}; } SecureRandom random = new SecureRandom(); context.init(km, tm, random); return context; }  static boolean isHttpsConnection(Map<String, ?> conf); static SSLContext createSSLContext(Map<String, ?> sslConfigurations, String sslAlgorithm); static Login initializeSecurityContext(Map<String, ?> conf);  }
@Test public void createSSLContext() throws Exception { String keyStorePath = "keystore.jks"; String keyStorePassword = "password"; String keyStoreType = "jks"; String trustStorePath = "trustsrore.jks"; String trustStorePassword = "password"; String trustStoreType = "jks"; Map<String, String> conf = new HashMap(); SSLContext sslContext = SecurityUtils.createSSLContext(conf, "TLS"); assertTrue(sslContext != null); conf.put("keyStorePath", keyStorePath); conf.put("keyStorePassword", keyStorePassword); conf.put("keyStoreType", keyStoreType); conf.put("trustStorePath", trustStorePath); conf.put("trustStorePassword", trustStorePassword); conf.put("trustStoreType", trustStoreType); sslContext = SecurityUtils.createSSLContext(conf, "TLS"); assertTrue(sslContext != null); }