Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect HTTP to HTTPS in spring cloud gateway

How to redirect http call to https in spring-cloud-gateway. I have configured https in my gateway project as described in This Link.

Now the HTTPS urls are working fine, but in addition I need to redirect all http calls to https. I have tried This

But this is not working for me, as I have not using default http port 8080 and also my application is running on spring security.

The following code is showing how to redirect http to https, but I need the same configuration for Netty server, as spring-cloud-gateway supports netty only..

@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**
                }
            }
        };
    }


}
like image 514
Arpan Das Avatar asked Nov 08 '22 05:11

Arpan Das


1 Answers

The best way to do this is by putting an apache in front of your gateway and having a rewrite rule on that. There are many advantages to running it this way. It also makes it so you have a static url that an end user can hit and if you have issues or change your underlying technology behind the apache, you can just point it to the new service/url or whatever. Doing it at the Apache also allows you to run things like mod-security. Ultimately it could just be a passthrough (with a rewrite rule for your http->https question) but this is better architecture than doing it in your Gateway with code.

like image 60
Mike Baglio Jr. Avatar answered Nov 27 '22 20:11

Mike Baglio Jr.