spring cloud gateway实现https访问
文章目录
前言
一、使用场景
二、使用步骤
1.下载阿里云SSL安全证书
2.证书配置
3.gateway服务设置后台微服务访问方式
总结
前言
通过配置spring cloud gateway实现服务网关https访问及下游服务的路由更改
一、使用场景
在某些第三方接口调用场景下需要提供https安全访问链接,例微信小程序的接口开发中,强制要求为https请求接口,本篇内容为通过阿里云SSL安全证书+spring cloud gateway访问配置实现服务接口的https安全访问。
二、使用步骤
1.下载阿里云SSL安全证书
登录个人账号信息,进入阿里云管理后台,点击跳转至SSL证书模块,选择免费证书(首次需要创建),点击下载,选择tomcat版本证书,下载的文件包含.pfx的证书文件和.txt的密码文件
2.证书配置
代码如下(示例):进入gateway网关服务,将下载的证书解压后添加至resource资源目录下,证书名称可进行自定义,然后在yaml配置中添加如下内容
server:ssl:enable: truekey-store: classpath: 自定义ssl证书文件key-store-type: PKCS12key-store-password: 阿里云下载证书对应密码
此时便可通过https访问到gateway服务
3.gateway服务设置后台微服务访问方式
通过上述配置步骤,虽然网关可以正常访问,但是在通过gateway访问其他服务时报错。由于进来时是https请求,在gateway转发给其他微服务时依然是https请求,这时可通过将其他服务也设置成https访问,即每个服务都进行配置ssl,同时采用域名进行注册服务,这无疑工作量很大。
使用过Zuul的都知道,Zuul默认会将https请求自动转为http请求给后台微服务,而gateway只需进行相应的配置便可实现同等功能。
第一种、修改gateway的配置:
spring:cloud:gateway:routes: #配置路由路径- id: oauth2-server# 之前路由为 uri: lb://oauth2-serveruri: lb:http://oauth2-server
第二种、添加过滤器(原理相同):
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;import java.net.URI;import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;import reactor.core.publisher.Mono;@Component
public class SchemeFilter implements GlobalFilter, Ordered {@Overridepublic Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {Object uriObj = exchange.getAttributes().get(GATEWAY_REQUEST_URL_ATTR);if (uriObj != null) {URI uri = (URI) uriObj;uri = this.upgradeConnection(uri, "http");exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);}return chain.filter(exchange);}private URI upgradeConnection(URI uri, String scheme) {UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri).scheme(scheme);if (uri.getRawQuery() != null) {// When building the URI, UriComponentsBuilder verify the allowed characters and does not// support the '+' so we replace it for its equivalent '%20'.// See issue https://jira.spring.io/browse/SPR-10172uriComponentsBuilder.replaceQuery(uri.getRawQuery().replace("+", "%20"));}return uriComponentsBuilder.build(true).toUri();}@Overridepublic int getOrder() {return 10101;}
}
总结
通过以上配置,便可实现gateway及gateway的https请求。
spring cloud gateway实现https访问相关推荐
- gateway配置https访问
1.下载根证书,此次测试为阿里云申请的免费证书,下载后解压如下: 2.将证书文件复制到resource目录 3.项目配置文件中配置: server:port: 8080ssl:enabled: tru ...
- spring cloud gateway之服务注册与发现
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在之前的文章介绍了Spring Cloud Gateway的Predict(断言).Filter( ...
- spring cloud gateway之filter篇
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理, ...
- Spring Cloud Gateway之Predict篇
Spring Cloud gateway工作流程 在之前的文章的Spring Cloud GateWay初体验中,大家已经对Spring Cloud Gateway的功能有一个初步的认识,网关作为一个 ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...
- 实战 Spring Cloud Gateway 之限流篇
来源:https://www.aneasystone.com/archives/2020/08/spring-cloud-gateway-current-limiting.html 话说在 Sprin ...
- Spring Cloud Gateway 入门
认识 Spring Cloud Gateway Spring Cloud Gateway 是一款基于 Spring 5,Project Reactor 以及 Spring Boot 2 构建的 API ...
- java版电子商务spring cloud分布式微服务b2b2c社交电商-spring cloud gateway之filter篇
社交电商平台源码请加企鹅求求:一零三八七七四六二六.filter的作用和生命周期 由filter工作流程点,可以知道filter有着非常重要的作用,在"pre"类型的过滤器可以做参 ...
- Spring Cloud Gateway 之请求坑位[微服务IP不同请求会失败]
问题产生背景 在使用Spring Cloud Gateway过程中,希望配置多Routes映射不同的微服务,因为Gateway 和Zuul的访问路径不同(zuul 会带有服务service Id),造 ...
最新文章
- What-If 工具:无需写代码,即可测试机器学习模型
- Android base64 上传图片
- linux shell 创建序列数组(list,array)方法
- cannot be deleted directly via the port API: has device owner network:floatingip
- 数据流图怎么画_概率图模型怎么画?5个步骤完成专业模型图
- 数据结构- 栈(实现综合计算器)(一位数计算 扩展到 多位数计算)
- el-select下拉框组件el-option如何使用v-for动态渲染问题 - 方法篇
- 《剑指Offer》 调整数组顺序使奇数位于偶数前面
- a href=javascript:;/a
- ASP.NET MVC2之Model Binder
- 第二阶段冲刺第六天站立会议
- Vijos 1041题:神风堂人数
- 【测试基础】Linux文本编辑vi命令
- UE4实时渲染——渲染前和遮挡
- Java8新特性之方法引用
- 【Proteus仿真】BME280温湿度气压传感器数据串口输出
- 【MySQL 数据库】聚合查询和联合查询操作
- 五线谱音名和组别对照表_钢琴音区名称及琴键名称对照表
- vue中使用keep-alive无效以及include 和 exclude用法
- linux手动安装rsync_在Linux/Unix上安装rsync并通过示例的方式介绍使用rsync命令
热门文章
- 这可能是东半球最详细的Linux下搭建Jenkins服务器实现自动打包的教程(下)
- 华为瘦胖ap互转_华为V2R3 胖瘦AP 转换
- STM32 嵌入式 超超超超超轻量级操作系统
- java 开源商城_让这个Java语言的开源商城系统火起来
- 电脑计算机无法运行怎么办,如果计算机在打开电源后仍无法运行,则该怎么办?计算机无法进入系统的原因[图形]...
- 十一小长假就快到了,忙忙碌碌大半年,消费黄金时期来啦
- 2020-12-17
- 服务器合租与虚拟主机的区别
- 安化云台山:中秋月圆夜,赏月正当时
- erfc函数、persistent、turbo码