无法使用Spring Boot 1.5.17在嵌入式Tomcat中配置Http 2

时间:2018-12-04 13:36:53

标签: spring spring-boot http2 embedded-tomcat-8 tomcat8.5

我正在尝试为在嵌入式Tomcat服务器8.5.34中运行的Spring Boot项目配置HTTP 2,但无法这样做。我关注了this SO Thread,但未能实现。我的服务器已启动,并且可以正常工作,但是即使在为HTTP 2配置后,它也可以在HTTP 1.1中运行。我已将项目配置为使用自签名证书运行,并且这些设置位于application.properties文件中。以下是我的配置文件。请帮忙。

@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;
    }
}

0 个答案:

没有答案