我正在尝试为在嵌入式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;
}
}