SSLSocketFactory FactoryBean

A Spring FactoryBean for an SSLSocketFactory.

License

License

MIT
GroupId

GroupId

com.github.marschall
ArtifactId

ArtifactId

ssl-socket-factory-factory-bean
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

SSLSocketFactory FactoryBean
A Spring FactoryBean for an SSLSocketFactory.
Project URL

Project URL

https://github.com/marschall/ssl-socket-factory-factory-bean
Source Code Management

Source Code Management

https://github.com/marschall/ssl-socket-factory-factory-bean

Download ssl-socket-factory-factory-bean

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.marschall/ssl-socket-factory-factory-bean/ -->
<dependency>
    <groupId>com.github.marschall</groupId>
    <artifactId>ssl-socket-factory-factory-bean</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.marschall/ssl-socket-factory-factory-bean/
implementation 'com.github.marschall:ssl-socket-factory-factory-bean:0.1.0'
// https://jarcasting.com/artifacts/com.github.marschall/ssl-socket-factory-factory-bean/
implementation ("com.github.marschall:ssl-socket-factory-factory-bean:0.1.0")
'com.github.marschall:ssl-socket-factory-factory-bean:jar:0.1.0'
<dependency org="com.github.marschall" name="ssl-socket-factory-factory-bean" rev="0.1.0">
  <artifact name="ssl-socket-factory-factory-bean" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.marschall', module='ssl-socket-factory-factory-bean', version='0.1.0')
)
libraryDependencies += "com.github.marschall" % "ssl-socket-factory-factory-bean" % "0.1.0"
[com.github.marschall/ssl-socket-factory-factory-bean "0.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
net.bytebuddy : byte-buddy Optional jar 1.10.20

provided (2)

Group / Artifact Type Version
org.springframework : spring-beans jar
org.springframework : spring-context jar

test (6)

Group / Artifact Type Version
org.springframework : spring-test jar
org.springframework : spring-web jar
org.junit.jupiter : junit-jupiter-api jar
org.junit.jupiter : junit-jupiter-engine jar
org.apache.logging.log4j : log4j-core jar
org.apache.logging.log4j : log4j-slf4j-impl jar

Project Modules

There are no modules declared in this project.

SSLSocketFactory FactoryBean Maven Central Build Status

A Spring FactoryBean for a SSLSocketFactory or SSLSocketFactory class.

Sometimes a framework or library does not support configuring SSL parameters like truststore, keystore, cipher suites or TLS versions directly but only by providing a javax.net.ssl.SSLSocketFactory instance or javax.net.ssl.SSLSocketFactory class. As javax.net.ssl.SSLSocketFactory is an abstract class a subclass has to be created that delegates to the implementation instance. This project aims to make this simpler by providing a Spring FactoryBean that takes care of this.

This project has an optional dependency on Byte Buddy which is needed when a SSLSocketFactory class rather than a SSLSocketFactory is desired, eg for com.sun.jndi.ldap.LdapCtx#SOCKET_FACTORY.

Usage

Define a bean of type SSLSocketFactoryFactoryBean and a bean of type SSLSocketFactory will be available in the application context.

@Configuration
public class SSLConfiguration {

  // there are various ways how configuration could happen, Spring properties is just one option
  @Value("${truststore.type}")
  private String truststoreType;

  @Value("${truststore.location}")
  private String truststoreLocation;

  @Value("${truststore.password}")
  private String truststorePassword;

  // define the SSLSocketFactoryFactoryBean
  @Bean
  FactoryBean<SSLSocketFactory> sslSocketFactory() {
    SSLSocketFactoryFactoryBean factoryBean = new SSLSocketFactoryFactoryBean();
    factoryBean.setTruststoreType(this.truststoreType);
    factoryBean.setTruststoreLocation(this.truststoreLocation);
    factoryBean.setTruststorePassword(this.truststorePassword);
    // these values could also be configurable
    factoryBean.setProtocol("TLSv1.2");
    factoryBean.setCipherSuites(Collections.singletonList("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"));
    return factoryBean;
  }

  @Bean
  RestOperations restTemplate(SSLSocketFactory sslSocketFactory /* created by the bean defined in #sslSocketFactory  */) {
    ClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() {
      @Override
      protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        if (connection instanceof HttpsURLConnection) {
          HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
          httpsConnection.setSSLSocketFactory(sslSocketFactory);
        }
        super.prepareConnection(connection, httpMethod);
      }
    };
    return new RestTemplate(requestFactory);
  }

}

Debugging

To easily verify that all configuration options are passed as desired use

-Djavax.net.debug=ssl:handshake

Versions

Version
0.1.0