Last active
February 12, 2016 15:03
-
-
Save jforge/7585c8d5d55ccf8332bd to your computer and use it in GitHub Desktop.
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
| import java.io.InputStream; | |
| import java.security.KeyStore; | |
| import javax.net.ssl.HostnameVerifier; | |
| import javax.net.ssl.SSLContext; | |
| import javax.net.ssl.SSLSession; | |
| import javax.net.ssl.TrustManager; | |
| import javax.net.ssl.TrustManagerFactory; | |
| public class SSLSupport { | |
| private String trustStoreResource; | |
| private String trustStorePassword; | |
| public SSLSupport() {} | |
| public SSLSupport(String trustStoreResource, String trustStorePassword) { | |
| this.trustStoreResource = trustStoreResource; | |
| this.trustStorePassword = trustStorePassword; | |
| } | |
| public HostnameVerifier createHostnameVerifier() { | |
| return new HostnameVerifier() { | |
| @Override | |
| public boolean verify(String hostname, SSLSession sslSession) { | |
| return true; | |
| } | |
| }; | |
| } | |
| public SSLContext createSSLContext() throws Exception { | |
| // TODO review, maybe this is a improved approach: SslConfigurator sslConfig = SslConfigurator.newInstance(); | |
| SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| sslContext.init(null, getTrustManager(), null); | |
| return sslContext; | |
| } | |
| private TrustManager[] getTrustManager() throws Exception { | |
| try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(trustStoreResource)) { | |
| KeyStore keyStore = KeyStore.getInstance("JKS"); | |
| keyStore.load(inputStream, trustStorePassword.toCharArray()); | |
| TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX"); | |
| trustManagerFactory.init(keyStore); | |
| TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | |
| return trustManagers; | |
| } | |
| } | |
| public void setTrustStoreResource(String trustStoreResource) { | |
| this.trustStoreResource = trustStoreResource; | |
| } | |
| public void setTrustStorePassword(String trustStorePassword) { | |
| this.trustStorePassword = trustStorePassword; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment