0

I am trying to configure HTTP 2 for my Spring Boot project that runs in the Embedded Tomcat Server 8.5.34 but I am unable to do so. I have followed this SO Thread but couldn't achieve that. My Server starts and is working fine but it works in HTTP 1.1 even after configuring for HTTP 2. I have configured my project to run with a self signed certificate and those setting are in the application.properties file. Below is my configuration file. Please help.

@Configuration
public class ConnectorConfig {

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };

        tomcat.addAdditionalTomcatConnectors(getHttpConnector());

        // the following line isn't working
        // tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.addUpgradeProtocol(new Http2Protocol()));
        return tomcat;
    }


    @Bean // not working
    public EmbeddedServletContainerCustomizer tomcatCustomizer() {
        return (container) -> {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                ((TomcatEmbeddedServletContainerFactory) container)
                        .addConnectorCustomizers((connector) -> connector.addUpgradeProtocol(new Http2Protocol()));
            }
        };
    }

    private Connector getHttpConnector() {
        Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(9000);

        // the following line isn't working
        // connector.addUpgradeProtocol(new Http2Protocol());
        return connector;
    }
}
5
  • 2
    I don't know what's wrong but there is out of the box support for HTTP/2 in SB2 so I'd upgrade if I were you. Commented Dec 4, 2018 at 13:44
  • @StephaneNicoll ok Stephane could you say briefly about how much my codes will affected if i upgrade. Mainly my project uses Spring MVC, Spring Data, Spring Elastic Search, Hibernate JPA.
    – sam
    Commented Dec 4, 2018 at 13:48
  • @StephaneNicoll i tried changing spring boot version to 2.0.7 from 1.5.17 and found that there are lots of changed in classes. So i cannot afford to do that now.
    – sam
    Commented Dec 4, 2018 at 14:24
  • 1
    You'll have to do it anyway, 1.5 is going EOL next August. If you have trouble upgrading, you can ask here or on the Gitter channel of the Spring Boot project. Commented Dec 5, 2018 at 9:42
  • @StephaneNicoll thank you so much.
    – sam
    Commented Dec 5, 2018 at 11:18

0