如何将http
呼叫重定向到https
in spring-cloud-gateway
。我已经在我的网关项目中配置了 https,如This Link中所述。
现在 HTTPS url 工作正常,但此外我需要将所有http
调用重定向到https
. 我试过这个
但这对我不起作用,因为我没有使用默认http
端口 8080,而且我的应用程序在 spring security 上运行。
以下代码显示如何重定向http
到https
,但我需要与Netty
服务器相同的配置,因为spring-cloud-gateway
仅支持 netty..
@Configuration
public class RedirectToHttpsConfig {
@Value("${server.port}")
private Integer httpsPort;
@Value("${server.http.port}")
private Integer httpPort;
/* ################ THIS WILL WORK FINE ################
################ IF YOU HAVE TOMCAT ################
################ AS EMBEDDED SERVER ################
*/
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
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(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(httpPort);
connector.setSecure(false);
connector.setRedirectPort(httpsPort);
return connector;
}
/* ################ -------- ################ */
/* ################ HOW TO DO THE ABOVE Configuration
FOR NETTY SERVER ################ */
@Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory(){
NettyReactiveWebServerFactory netty = nettyReactiveWebServerFactory(){
NettyServerCustomizer nettyServerCustomizer = new NettyServerCustomizer() {
@Override
public void customize(HttpServerOptions.Builder builder) {
**// NOT ABLE TO FIGURE OUT HERE**
}
}
};
}
}