如何在Spring Cloud Gateway中将HTTP重定向到HTTPS

时间:2018-07-03 08:42:03

标签: spring-cloud-gateway

如何将http的呼叫https重定向到spring-cloud-gateway中的http。我已经按照This Link中的说明在网关项目中配置了https。

现在,HTTPS URL可以正常工作,但是除此之外,我需要将所有https调用重定向到http。我已经尝试过This

但这对我不起作用,因为我没有使用默认的http端口8080,而且我的应用程序还运行在Spring Security上。

以下代码显示了如何将https重定向到Netty,但是我需要spring-cloud-gateway服务器具有相同的配置,因为@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** } } }; } } 仅支持netty。 >

  void TestBitmap()
    {
        var startBytes = new byte[256];
        for (int i = 0; i < startBytes.Length; i++)
        {
            startBytes[i] = (byte)i;
        }
        SaveRawBufferToBitmap(startBytes, "1 - PreLoad.bmp");

        var bitmap = new Bitmap("1 - PreLoad.bmp");
        if (File.Exists("Bitmap.txt"))
            File.Delete("Bitmap.txt");
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 16; j++)
            {
                File.AppendAllText("Bitmap.txt", $"{bitmap.GetPixel(j, i)}{Environment.NewLine}");
            }
        }

        var bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        int numbytes = bmpdata.Stride * bitmap.Height;
        byte[] bytes = new byte[numbytes];
        IntPtr ptr = bmpdata.Scan0;
        Marshal.Copy(ptr, bytes, 0, numbytes);
        bitmap.UnlockBits(bmpdata);

        if (File.Exists("Bytes.txt"))
            File.Delete("Bytes.txt");
        for (int i = 0; i < bytes.Length; i++)
        {
            File.AppendAllText("Bytes.txt", $"{bytes[i]} ");
        }

        SaveRawBufferToBitmap(bytes, "2 - PostLoad.bmp");
    }


    public static void SaveRawBufferToBitmap(byte[] buffer, string path)
    {
        WriteableBitmap imageWritable;
        var rect = new Int32Rect(0, 0, 16, 16);
        var imageRotated = new byte[256];

        if (File.Exists(path))
            File.Delete(path);

        imageWritable = new WriteableBitmap(16, 16, 96, 96, System.Windows.Media.PixelFormats.Gray8, null);
        imageWritable.WritePixels(rect, buffer, 16, 0);
        using (FileStream stream = new FileStream(path, FileMode.Create))
        {
            var encoder = new TiffBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(imageWritable));
            encoder.Save(stream);
            stream.Close();
        }
    }

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是在网关前面放置一个Apache,并对此进行重写。以这种方式运行有很多优点。它还使您拥有一个可供最终用户访问的静态URL,如果您遇到问题或更改了Apache背后的基础技术,则可以将其指向新的服务/ URL或其他任何东西。在Apache上执行此操作还使您可以运行mod-security之类的东西。最终,它可能只是一个传递(具有针对您的http-> https问题的重写规则),但这比在网关中使用代码来实现更好的体系结构。