Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sandipchitale/81d8956246dac9957cd5a6edeefa2b68 to your computer and use it in GitHub Desktop.
Save sandipchitale/81d8956246dac9957cd5a6edeefa2b68 to your computer and use it in GitHub Desktop.
SslBundleRegistrar based on classic ServerProperties #SslBundleRegistrar #ServerProperties
package sandipchitale.twowaytlsserver;
import org.springframework.boot.autoconfigure.ssl.SslBundleRegistrar;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundleRegistry;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.boot.ssl.jks.JksSslStoreBundle;
import org.springframework.boot.ssl.jks.JksSslStoreDetails;
/**
* Registers the SSL bundle from {@link ServerProperties} with the name. If the name is not specified it uses
* {@link #SERVER_PROPERTIES_SSL_BUNDLE_NAME} for the bundle name.
*
* You can use this class as a bean in your application to register the SSL bundle with the name based on {@link ServerProperties}.
*
* @Bean
* public ServerPropertiesSslBundleRegistrar serverPropertiesSslBundleRegistrar(ServerProperties serverProperties) {
* return new ServerPropertiesSslBundleRegistrar("tomcat", serverProperties);
* }
*
* Later you can retrieve the sslBundle with name "tomcat" from {@link SslBundles} as follows:
*
* SslBundle sslBundle = SslBundles.get("tomcat");
*
*/
public class ServerPropertiesSslBundleRegistrar implements SslBundleRegistrar {
public static final String SERVER_PROPERTIES_SSL_BUNDLE_NAME = "SERVER_PROPERTIES_SSL_BUNDLE";
private final String serverSslBundleName;
private final ServerProperties serverProperties;
private final SslBundle sslBundle;
public ServerPropertiesSslBundleRegistrar(ServerProperties serverProperties) {
this(SERVER_PROPERTIES_SSL_BUNDLE_NAME, serverProperties);
}
public ServerPropertiesSslBundleRegistrar(String serverSslBundleName, ServerProperties serverProperties) {
this.serverSslBundleName = serverSslBundleName;
this.serverProperties = serverProperties;
this.sslBundle = createSslBundler(serverProperties);
}
public static SslBundle createSslBundler(ServerProperties serverProperties) {
JksSslStoreDetails keyStoreDetails = new JksSslStoreDetails(
serverProperties.getSsl().getKeyStoreType(),
serverProperties.getSsl().getKeyStoreProvider(),
serverProperties.getSsl().getKeyStore(),
serverProperties.getSsl().getKeyStorePassword()
);
JksSslStoreDetails trustStoreDetails = new JksSslStoreDetails(
serverProperties.getSsl().getTrustStoreType(),
serverProperties.getSsl().getTrustStoreProvider(),
serverProperties.getSsl().getTrustStore(),
serverProperties.getSsl().getTrustStorePassword()
);
return SslBundle.of(new JksSslStoreBundle(keyStoreDetails, trustStoreDetails));
}
public String getServerSslBundleName() {
return serverSslBundleName;
}
public ServerProperties getServerProperties() {
return serverProperties;
}
public SslBundle getSslBundle() {
return sslBundle;
}
@Override
public void registerBundles(SslBundleRegistry registry) {
registry.registerBundle(serverSslBundleName, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment