我的 SpringBoot 应用程序已配置为在 https 端口上运行。它工作正常。现在我想将流量从 HTTP 端口自动重定向到 HTTPS 端口。该代码也可以正常工作 - 但仅限于 Firefox。为什么 Safari 不自动重定向?
在 Safari 上点击 HTTP URL:PORT,会在服务器控制台上显示奇怪的日志 - 方法名称中的非法字符等。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* This part is option & used only for this particular use case.
*
* We want the http port 8080 redirect automatically to the https port 8443.
*
*/
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext (Context context) {
SecurityConstraint securityConstriant=new SecurityConstraint();
securityConstriant.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstriant.addCollection(collection);
context.addConstraint(securityConstriant);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setRedirectPort(8443);
return connector;
}
}