java - Spring Boot 2 中缺少 TomcatEmbeddedServletContainerFactory

标签 java spring spring-boot

我有 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

最佳答案

在 Spring 引导中 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;
}

关于java - Spring Boot 2 中缺少 TomcatEmbeddedServletContainerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718650/

相关文章:

java - 尽管做得正确但仍出现空指针异常

java - 将预先编写的 SpringMVC + Hibernate 部署到 AWS

java - 无效的列名异常 - 带别名的 JdbcPagingItemReader 查询

java - 如何更改 Android java/xml 中的 numberPicker 速度

spring - 找不到 CacheableOperation[] 缓存的名为 '' 的缓存

java - Mockito 与 JavaMailSender

java - 使用 spring security 根据用户角色设置自定义登录后目标

spring-boot - 需要找不到类型为 'org.springframework.security.core.userdetails.UserDetailsService'的bean

java - Spring REST - 正在下载损坏/空白文件

java - 哪里用callable,哪里用Runnable接口(interface)?