我试图为我的SpringBoot应用程序设置3个Tomcat连接器:
最后2个我按照this tutorial在单独的配置类中进行配置 我稍微修改了它以配置纯http连接器。
这最后2个很难。它们单独工作正常,但在将它们配置在一起时,我需要选择具有破坏纯http连接器的安全约束,或者没有破坏重定向的约束。 显然,春天讨厌有趣,并且不让我拥有2个因子:/
有没有办法将不同的SecurityConstraints应用于不同的连接器?
我将包含我的配置类,以便你们了解我到目前为止所拥有的内容:
@Configuration
public class RedirectConfig {
@Value("${server.alt-port}")
private int altPort;
@Value("${server.port}")
private int port;
@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); //<-- I must add it for the entire factory or not at all
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
tomcat.addAdditionalTomcatConnectors(initiateRedirectHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(altPort);
connector.setSecure(false);
return connector;
}
private Connector initiateRedirectHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(port);
return connector;
}
}