Spring Cloud Gateway同时监听HTTP和HTTPS(http自动转发https端口)

本文介绍如何在Spring Cloud Gateway中同时监听HTTP和HTTPS端口,并实现HTTP请求自动转发到HTTPS。通过配置两个Netty服务器,分别监听8080和8443端口,8080端口的请求被重定向至8443。测试使用Java 1.8、Spring Boot 2.2.1.RELEASE和Spring Cloud Gateway Hoxton.RC1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot默认采用Tomcat作为内嵌容器,通过设置可以轻松实现同时监听Http和https两个端口,http自动转发给https端口。然而,SpringCloud Gateway由于默认使用netty作为内嵌web容器,并且官方手册内,只有说明如何设置Https,却没有说明如何设置同时监听多个端口,并且http自动转发到https端口。

虽然作为普通微服务,这个功能并不是很重要,但是作为网关,http自动转发给https端口,是一个比较实用的功能。在网上寻觅了一波,还是找到了解决方案,但并不是官方的,但是可以实现功能。后面持续关注,官方可能会给出解决方案。

参考:
https://stackoverflow.com/questions/49045670/spring-webflux-redirect-http-to-https/53000573#53000573
https://github.com/spring-projects/spring-boot/issues/12035


1.整体结构图 

### Spring Cloud Gateway HTTPSHTTP 配置 在 Spring Cloud Gateway 中实现将 HTTPS 请求转换为 HTTP 请求的功能,可以通过自定义路由规则以及修改网关的行为来完成。以下是具体的配置方法: #### 1. 修改 `application.yml` 或 `application.properties` 为了使 Spring Cloud Gateway 将外部的 HTTPS 流量内部重定向到 HTTP 协议的服务端口上,可以调整如下属性: ```yaml server: port: 8443 # 设置服务器监听HTTPS 端口spring: cloud: gateway: routes: - id: http_to_https_route uri: http://localhost:8080 # 目标服务地址 (HTTP) predicates: - Path=/example/** # 定义匹配路径 ``` 上述配置中指定了一个名为 `http_to_https_route` 的路由规则[^5],它会拦截 `/example/` 开头的所有请求并将这些请求转发至目标 URI (`http://localhost:8080`)。 #### 2. 启用 SSL 支持 为了让 Spring Cloud Gateway 接收 HTTPS 请求,在应用启动时需加载证书文件。这通常涉及以下步骤: - **添加依赖项** 如果尚未引入必要的安全库,则需要更新项目的 Maven POM 文件以包含 Jetty 或 Tomcat 提供的支持包之一: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- 如果使用 Jetty --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> </dependency> ``` - **配置 keystore truststore 参数** 接着,在 `application.yml` 中指定密钥存储位置及其密码信息: ```yaml server: ssl: key-store-type: PKCS12 key-store: classpath:keystore.p12 key-password: secret enabled: true ``` 此部分设置了用于加密通信的数据源,并启用了 SSL 功能[^6]。 #### 3. 自定义过滤器处理协议切换逻辑 有时仅靠简单的 URL 映射无法满足需求场景下的复杂业务流程控制;此时可通过编写自定义全局过滤器或者局部过滤器进一步增强功能特性。下面展示了一个例子——创建一个新的类继承抽象基类 AbstractGatewayFilterFactory<T> 来实现特定行为模式: ```java import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; public class ProtocolConversionFilter extends AbstractGatewayFilterFactory<ProtocolConversionFilter.Config> { public static class Config { private String targetScheme = "http"; public void setTargetScheme(String scheme){ this.targetScheme=scheme; } public String getTargetScheme(){ return this.targetScheme; } } @Override public GatewayFilter apply(Config config) { return ((exchange, chain) -> { exchange.getRequest().mutate() .scheme(config.getTargetScheme()) .build(); return chain.filter(exchange); }); } } ``` 之后就可以像其他预设好的工厂那样注册该实例对象了[^7]: ```yaml spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET - POST default-filters: - name: ProtocolConversionFilter args: _targetScheme_: 'http' ``` 以上代码片段展示了如何构建一个能够改变原始请求 Scheme 属性值的新类型 Filter 并将其应用于整个应用程序之中。 --- ###
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值