一尘不染

Spring Boot 2中缺少TomcatEmbeddedServletContainerFactory

spring

我有Spring Boot应用程序版本1.5.x,该版本使用org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory,试图将其迁移到Spring Boot 2,但是该应用程序无法编译,尽管它具有对的依赖org.springframework.boot:spring-boot-starter-tomcat。编译器发出以下错误:

error: package org.springframework.boot.context.embedded.tomcat

阅读 2749

收藏
2020-04-19

共1个答案

一尘不染

在Spring boot 2.0.0.RELEASE中,你可以替换为以下代码:

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @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(redirectConnector());
    return tomcat;
}

private Connector redirectConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
}
2020-04-19