public final class SecureConnections extends Object
Method getSocketFactoryTrusting(Path, String)
highly simplifies
trusting custom certificates by creating a SSLSocketFactory
directly
from a JKS (Java Key Store) file. Those key stores can be created and
maintained using e.g. the KeyStore
Explorer.
Path jksFilePath = ...; String password = ...; SSLSocketFactory socketFactory = SecureConnections.getSocketFactoryTrusting(jksFilePath, password); URL url = ...; URLConnection urlConnection = url.openConnection(); SecureConnections.getHttpsUrlConnection(urlConnection) .ifPresent(httpsUrlConnection -> httpsUrlConnection.setSSLSocketFactory(socketFactory));
Modifier and Type | Method and Description |
---|---|
static Optional<HttpsURLConnection> |
getHttpsUrlConnection(URLConnection connection)
|
static KeyManager[] |
getKeyManagers(KeyStore keyStore,
String password)
Returns an array of
KeyManager s for a given key store and password. |
static SSLSocketFactory |
getSocketFactory(KeyManager[] keyManagers)
Creates a
SSLSocketFactory based on key managers. |
static SSLSocketFactory |
getSocketFactory(KeyManager[] keyManagers,
TrustManager[] trustManagers)
Creates a
SSLSocketFactory based on key managers and trust managers. |
static SSLSocketFactory |
getSocketFactory(TrustManager[] trustManagers)
Creates a
SSLSocketFactory based on trust managers. |
static SSLSocketFactory |
getSocketFactoryTrusting(Path jksFilePath,
String password)
Creates a
SSLSocketFactory trusting the certificates inside JKS (Java
Key Store) file. |
static TrustManager[] |
getTrustManagers(KeyStore keyStore)
Returns an array of
TrustManager s for a given key store and password. |
static KeyStore |
loadKeyStore(Path jksFilePath,
String password)
Loads a JKS (Java Key Store) file.
|
public static Optional<HttpsURLConnection> getHttpsUrlConnection(URLConnection connection)
connection
- the connection to handleconnection
if it is of type HttpsURLConnection
or
Optional.empty()
public static KeyManager[] getKeyManagers(KeyStore keyStore, String password) throws UnrecoverableKeyException
KeyManager
s for a given key store and password.keyStore
- key storepassword
- key store passwordKeyManager
sUnrecoverableKeyException
- if the key cannot be recovered (e.g. the
given password is wrong).public static SSLSocketFactory getSocketFactory(KeyManager[] keyManagers)
SSLSocketFactory
based on key managers.keyManagers
- key managerpublic static SSLSocketFactory getSocketFactory(TrustManager[] trustManagers)
SSLSocketFactory
based on trust managers.trustManagers
- trust managerpublic static SSLSocketFactory getSocketFactory(@Nullable KeyManager[] keyManagers, @Nullable TrustManager[] trustManagers)
SSLSocketFactory
based on key managers and trust managers.keyManagers
- key managertrustManagers
- trust managerpublic static SSLSocketFactory getSocketFactoryTrusting(Path jksFilePath, String password) throws CertificateException, IOException
SSLSocketFactory
trusting the certificates inside JKS (Java
Key Store) file.jksFilePath
- path to the key storepassword
- key store passwordCertificateException
- if any of the certificates in the key store
could not be loadedIOException
- if an I/O error occurspublic static TrustManager[] getTrustManagers(KeyStore keyStore)
TrustManager
s for a given key store and password.keyStore
- key storeTrustManager
spublic static KeyStore loadKeyStore(Path jksFilePath, String password) throws CertificateException, IOException
jksFilePath
- path to the key storepassword
- key store passwordCertificateException
- if any of the certificates in the key store
could not be loadedIOException
- if an I/O error occursCopyright © 2025. All rights reserved.