Class SslConfigurator


  • public final class SslConfigurator
    extends Object
    Utility class, which helps to configure SSLContext instances. For example:
     SslConfigurator sslConfig = SslConfigurator.newInstance()
        .trustStoreFile("truststore.jks")
        .trustStorePassword("asdfgh")
        .trustStoreType("JKS")
        .trustManagerFactoryAlgorithm("PKIX")
    
        .keyStoreFile("keystore.jks")
        .keyPassword("asdfgh")
        .keyStoreType("JKS")
        .keyManagerFactoryAlgorithm("SunX509")
        .keyStoreProvider("SunJSSE")
    
        .securityProtocol("SSL");
    
     SSLContext sslContext = sslConfig.createSSLContext();
     
    Author:
    Alexey Stashok, Hubert Iwaniuk, Bruno Harbulot, Marek Potociar
    • Field Detail

      • TRUST_STORE_PROVIDER

        public static final String TRUST_STORE_PROVIDER
        Trust store provider name. The value MUST be a String representing the name of a trust store provider.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.trustStoreProvider".

        See Also:
        Constant Field Values
      • KEY_STORE_PROVIDER

        public static final String KEY_STORE_PROVIDER
        Key store provider name. The value MUST be a String representing the name of a trust store provider.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.keyStoreProvider".

        See Also:
        Constant Field Values
      • TRUST_STORE_FILE

        public static final String TRUST_STORE_FILE
        Trust store file name. The value MUST be a String representing the name of a trust store file.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.trustStore".

        See Also:
        Constant Field Values
      • KEY_STORE_FILE

        public static final String KEY_STORE_FILE
        Key store file name. The value MUST be a String representing the name of a key store file.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.keyStore".

        See Also:
        Constant Field Values
      • TRUST_STORE_PASSWORD

        public static final String TRUST_STORE_PASSWORD
        Trust store file password - the password used to unlock the trust store file. The value MUST be a String representing the trust store file password.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.trustStorePassword".

        See Also:
        Constant Field Values
      • KEY_STORE_PASSWORD

        public static final String KEY_STORE_PASSWORD
        Key store file password - the password used to unlock the trust store file. The value MUST be a String representing the key store file password.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.keyStorePassword".

        See Also:
        Constant Field Values
      • TRUST_STORE_TYPE

        public static final String TRUST_STORE_TYPE
        Trust store type (see KeyStore.getType() for more info). The value MUST be a String representing the trust store type name.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.trustStoreType".

        See Also:
        Constant Field Values
      • KEY_STORE_TYPE

        public static final String KEY_STORE_TYPE
        Key store type (see KeyStore.getType() for more info). The value MUST be a String representing the key store type name.

        No default value is set.

        The name of the configuration property is "javax.net.ssl.keyStoreType".

        See Also:
        Constant Field Values
      • KEY_MANAGER_FACTORY_ALGORITHM

        public static final String KEY_MANAGER_FACTORY_ALGORITHM
        Key manager factory algorithm name. The value MUST be a String representing the key manager factory algorithm name.

        No default value is set.

        The name of the configuration property is "ssl.keyManagerFactory.algorithm".

        See Also:
        Constant Field Values
      • KEY_MANAGER_FACTORY_PROVIDER

        public static final String KEY_MANAGER_FACTORY_PROVIDER
        Key manager factory provider name. The value MUST be a String representing the key manager factory provider name.

        No default value is set.

        The name of the configuration property is "ssl.keyManagerFactory.provider".

        See Also:
        Constant Field Values
      • TRUST_MANAGER_FACTORY_ALGORITHM

        public static final String TRUST_MANAGER_FACTORY_ALGORITHM
        Trust manager factory algorithm name. The value MUST be a String representing the trust manager factory algorithm name.

        No default value is set.

        The name of the configuration property is "ssl.trustManagerFactory.algorithm".

        See Also:
        Constant Field Values
      • TRUST_MANAGER_FACTORY_PROVIDER

        public static final String TRUST_MANAGER_FACTORY_PROVIDER
        Trust manager factory provider name. The value MUST be a String representing the trust manager factory provider name.

        No default value is set.

        The name of the configuration property is "ssl.trustManagerFactory.provider".

        See Also:
        Constant Field Values
    • Method Detail

      • getDefaultContext

        public static SSLContext getDefaultContext()
        Get a new instance of a SSLContext configured using default configuration settings. The default SSL configuration is initialized from system properties. This method is a shortcut for getDefaultContext(true).
        Returns:
        new instance of a default SSL context initialized from system properties.
      • getDefaultContext

        public static SSLContext getDefaultContext​(boolean readSystemProperties)
        Get a new instance of a SSLContext configured using default configuration settings. If readSystemProperties parameter is set to true, the default SSL configuration is initialized from system properties.
        Parameters:
        readSystemProperties - if true, the default SSL context will be initialized using system properties.
        Returns:
        new instance of a default SSL context initialized from system properties.
      • newInstance

        public static SslConfigurator newInstance()
        Get a new & initialized SSL configurator instance. The the instantiated configurator will be empty.
        Returns:
        new & initialized SSL configurator instance.
      • newInstance

        public static SslConfigurator newInstance​(boolean readSystemProperties)
        Get a new SSL configurator instance.
        Parameters:
        readSystemProperties - if true, Retrieves the initial configuration from System.getProperty(String)}, otherwise the instantiated configurator will be empty.
        Returns:
        new SSL configurator instance.
      • copy

        public SslConfigurator copy()
        Create a copy of the current SSL configurator instance.
        Returns:
        copy of the current SSL configurator instance
      • trustStoreProvider

        public SslConfigurator trustStoreProvider​(String trustStoreProvider)
        Set the trust store provider name.
        Parameters:
        trustStoreProvider - trust store provider to set.
        Returns:
        updated SSL configurator instance.
      • keyStoreProvider

        public SslConfigurator keyStoreProvider​(String keyStoreProvider)
        Set the key store provider name.
        Parameters:
        keyStoreProvider - key store provider to set.
        Returns:
        updated SSL configurator instance.
      • trustStoreType

        public SslConfigurator trustStoreType​(String trustStoreType)
        Set the type of trust store.
        Parameters:
        trustStoreType - type of trust store to set.
        Returns:
        updated SSL configurator instance.
      • keyStoreType

        public SslConfigurator keyStoreType​(String keyStoreType)
        Set the type of key store.
        Parameters:
        keyStoreType - type of key store to set.
        Returns:
        updated SSL configurator instance.
      • trustStorePassword

        public SslConfigurator trustStorePassword​(String password)
        Set the password of trust store.
        Parameters:
        password - password of trust store to set.
        Returns:
        updated SSL configurator instance.
      • keyStorePassword

        public SslConfigurator keyStorePassword​(String password)
        Set the password of key store.
        Parameters:
        password - password of key store to set.
        Returns:
        updated SSL configurator instance.
      • keyStorePassword

        public SslConfigurator keyStorePassword​(char[] password)
        Set the password of key store.
        Parameters:
        password - password of key store to set.
        Returns:
        updated SSL configurator instance.
      • keyPassword

        public SslConfigurator keyPassword​(String password)
        Set the password of the key in the key store.
        Parameters:
        password - password of key to set.
        Returns:
        updated SSL configurator instance.
      • keyPassword

        public SslConfigurator keyPassword​(char[] password)
        Set the password of the key in the key store.
        Parameters:
        password - password of key to set.
        Returns:
        updated SSL configurator instance.
      • trustStoreBytes

        public SslConfigurator trustStoreBytes​(byte[] payload)
        Set the trust store payload as byte array.

        Setting a trust store instance resets any trust store file or trust store instance value previously set.

        Parameters:
        payload - trust store payload.
        Returns:
        updated SSL configurator instance.
      • keyStoreBytes

        public SslConfigurator keyStoreBytes​(byte[] payload)
        Set the key store payload as byte array.

        Setting a key store instance resets any key store file or key store instance value previously set.

        Parameters:
        payload - key store payload.
        Returns:
        updated SSL configurator instance.
      • trustManagerFactoryAlgorithm

        public SslConfigurator trustManagerFactoryAlgorithm​(String algorithm)
        Set the trust manager factory algorithm.
        Parameters:
        algorithm - the trust manager factory algorithm.
        Returns:
        updated SSL configurator instance.
      • keyManagerFactoryAlgorithm

        public SslConfigurator keyManagerFactoryAlgorithm​(String algorithm)
        Set the key manager factory algorithm.
        Parameters:
        algorithm - the key manager factory algorithm.
        Returns:
        updated SSL configurator instance.
      • trustManagerFactoryProvider

        public SslConfigurator trustManagerFactoryProvider​(String provider)
        Set the trust manager factory provider.
        Parameters:
        provider - the trust manager factory provider.
        Returns:
        updated SSL configurator instance.
      • keyManagerFactoryProvider

        public SslConfigurator keyManagerFactoryProvider​(String provider)
        Set the key manager factory provider.
        Parameters:
        provider - the key manager factory provider.
        Returns:
        updated SSL configurator instance.
      • securityProtocol

        public SslConfigurator securityProtocol​(String protocol)
        Set the SSLContext protocol. The default value is TLS if this is null.
        Parameters:
        protocol - protocol for SSLContext.getProtocol().
        Returns:
        updated SSL configurator instance.
      • keyStore

        public SslConfigurator keyStore​(KeyStore keyStore)
        Set the key store instance.

        Setting a key store instance resets any key store file or key store payload value previously set.

        Parameters:
        keyStore - key store instance.
        Returns:
        updated SSL configurator instance.
      • trustStore

        public SslConfigurator trustStore​(KeyStore trustStore)
        Set the trust store instance.
        Parameters:
        trustStore - trust store instance.
        Returns:
        updated SSL configurator instance.
      • createSSLContext

        public SSLContext createSSLContext()
        Create new SSL context instance using the current SSL context configuration.
        Returns:
        newly configured SSL context instance.
      • retrieve

        public SslConfigurator retrieve​(Properties props)
        Retrieve the SSL context configuration from the supplied properties.
        Parameters:
        props - properties containing the SSL context configuration.
        Returns:
        updated SSL configurator instance.
      • retrieve

        public SslConfigurator retrieve()
        Retrieve the SSL context configuration from the system properties.
        Returns:
        updated SSL configurator instance.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object