Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot customize Jetty SSLContextFactory

From reading the spring-boot docs, it seems like the standard way to customize the Jetty server is to implement a class like the following:

  @Component
  public class JettyServerCustomizer 
      implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {

  @Autowired
  private ServerProperties serverProperties;

  @Override
  public void customize(final JettyServletWebServerFactory factory) {
      factory.addServerCustomizers((server) -> {
          // Customize 
      });
  }

}

I'm specifically interested in modifying the SSLContextFactory.

Tracing through the spring-boot code, right before the customizers are called, ssl is configured:

if (getSsl() != null && getSsl().isEnabled()) {
    customizeSsl(server, address);
}
for (JettyServerCustomizer customizer : getServerCustomizers()) {
    customizer.customize(server);
}

customizeSsl is a private method so cannot be overridden easily:

private void customizeSsl(Server server, InetSocketAddress address) {
    new SslServerCustomizer(address, getSsl(), getSslStoreProvider(), getHttp2()).customize(server);
}

One option is to create the context factory and connector ourselves in the customizer, and then overwrite the connectors on the server. This would probably work but it feels like we are re-creating a bunch of code that spring-boot is already doing just to be able to call a method on the SSLContextFactory.

It seems like if we could somehow provider our own SslServerCustomizer then we could do the custom configuration we want.

Does anyone know of a better way to do this?

like image 528
Bryan Bende Avatar asked Sep 12 '26 02:09

Bryan Bende


1 Answers

On my case it works just fine as:

@SpringBootApplication
@ComponentScan(basePackages = { "org.demo.jetty.*" })
public class DemoWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoWebApplication.class, args);
    }

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
        factory.setContextPath("/demo-app");
        factory.addServerCustomizers(getJettyConnectorCustomizer());
        return factory;
    }

    private JettyServerCustomizer getJettyConnectorCustomizer() {
        return server -> {

            final HttpConfiguration httpConfiguration = new HttpConfiguration();
            httpConfiguration.setSecureScheme("https");
            httpConfiguration.setSecurePort(44333);

            SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
            sslContextFactory.setKeyStoreType("PKCS12");
            sslContextFactory.setKeyStorePath("C:/jetty-demo/demo_cert.p12");
            sslContextFactory.setKeyStorePassword("*****");
            sslContextFactory.setKeyManagerPassword("****");

            final HttpConfiguration httpsConfiguration = new HttpConfiguration(httpConfiguration);
            httpsConfiguration.addCustomizer(new SecureRequestCustomizer());
            ServerConnector httpsConnector = new ServerConnector(server,
                    new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
                    new HttpConnectionFactory(httpsConfiguration));
            httpsConnector.setPort(44333);

            server.setConnectors(new Connector[] { httpsConnector });
            server.setStopAtShutdown(true);
            server.setStopTimeout(5_000);
        };
    }
}

You can define also a HTTP connector and add it to the customized section

...
ServerConnector connector = new ServerConnector(server);
    connector.addConnectionFactory(new HttpConnectionFactory(httpConfiguration));        
    connector.setPort(8081);

server.setConnectors(new Connector[]{connector, httpsConnector});
...
like image 189
alberto Avatar answered Sep 13 '26 18:09

alberto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!