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