为了使Tomcat执行重定向,您需要使用一个或多个安全约束对其进行配置。您可以通过
Context使用
TomcatEmbeddedServletContainerFactory子类对进行后期处理来实现。
例如:
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); }};由于
CONFIDENTIAL和
/*,这将导致Tomcat将每个请求重定向到HTTPS。如果需要对什么是重定向和不重定向进行更多控制,则可以配置多个模式和多个约束。



