当前位置: 首页 > news >正文

网关(Gateway)- 内置过滤器工厂

官方文档:Spring Cloud Gateway

内置过滤器工厂

AddRequestHeaderGatewayFilterFactory

为请求添加Header Header的名称及值

配置说明

server:port: 8088
spring:application:name: api-gatewaycloud:nacos:discovery:server-addr: 127.0.0.1:8847username: nacospassword: nacosgateway:routes:- id: order_route # 路由唯一标识#uri: http://localhost:8020 # 需要转发的地址uri: lb://order-service # 需要转发的地址# 断言规则  用于路由规则的匹配predicates:
#            - Path=/order-serv/**
#            - After=2024-01-01T23:00:11.518+08:00[Asia/Shanghai]
#            - Before=2025-01-01T23:00:11.518+08:00[Asia/Shanghai]- Between=2024-01-01T23:00:11.518+08:00[Asia/Shanghai],2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
#            - Header=X-Request-Id,\d+
#            - Host=127.0.0.1
#            - Query=name,lq
#            - Method=GET,POST# http://localhost:8088/order-serv/order/add  => http://localhost:8020/order-serv/order/add- Demo=YES#配置过滤器工厂filters:- StripPrefix=1 # 转发去掉第一层路径- AddRequestHeader=X-Request-name,tom #添加请求头# http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add

代码编写

package com.learning.springcloud.gateway.controller;import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;@RestController
@Slf4j
@RequestMapping("/gateway")
public class GatewayController {@GetMapping("/test")public String testGateway(HttpServletRequest request) throws Exception { return "success => gateWay-test 获取请求头X-Request-name:" + request.getHeader("X-Request-name");}@GetMapping("/test2")public String testGateway(@RequestHeader("X-Request-name") String name) throws Exception {return "success => gateWay-test2 获取请求头X-Request-name:" + name;}}

访问效果

AddRequestParameterGatewayFilterFactory

为请求添加参数的名称及值

配置说明

 filters:- StripPrefix=1 # 转发去掉第一层路径- AddRequestHeader=X-Request-name,tom #添加请求头- AddRequestParameter=color, blue # 添加请求参数

代码编写 

    @GetMapping("/test3")public String testGateway3(@RequestParam("color") String color) throws Exception {log.info("gateWay-get 获取请求参数color:" + color);return "success => gateWay-get 获取请求参数color:" + color;}

访问效果

 PrefixPathGatewayFilterFactory

增加项目路径的过滤器

配置说明

# order-service 服务配置
server:port: 8020servlet:context-path: /demo# 网关配置filters:- StripPrefix=1 # 转发去掉第一层路径- AddRequestHeader=X-Request-name,tom #添加请求头- AddRequestParameter=color, blue # 添加请求参数- PrefixPath=/demo

访问效果 

  • 不携带 配置的项目路径 demo 是无法访问的

  • 携带配置的项目路径demo是可以访问的 

  •  通过网关访问不携带项目路径demo 可以正常访问

 RedirectToGatewayFilterFactory

重定向过滤器,两个参数:HTTP状态码 和 重定向的目标地址

配置说明

      #配置过滤器工厂filters:- StripPrefix=1 # 转发去掉第一层路径- AddRequestHeader=X-Request-name,tom #添加请求头- AddRequestParameter=color, blue # 添加请求参数- PrefixPath=/demo- RedirectTo=302, https://www.baidu.com/ #重定向到百度

访问效果 

http://www.lryc.cn/news/364356.html

相关文章:

  • 电风扇如何实现跌倒断电保护功能
  • 编译原理总结
  • JavaScript:从基础到进阶的全面介绍
  • linux指令-sed
  • Docker部署青龙面板
  • 【LeetCode】每日一题 2024_6_4 将元素分配到两个数组中 II(二分、离散化、树状数组)
  • JAVA小案例-break练习,随机数,到88停止
  • C++第三方库【httplib】断点续传
  • [SaaS] AI+数据,tiktok选品,找达人,看广告数据
  • A股冲高回落,金属、地产板块领跌,新股N汇成真首日暴涨753%
  • dns域名解析服务和bond网卡
  • 视频生成框架EasyAnimate正式开源!
  • 【微机原理与汇编语言】并行接口8255实验
  • Oracle表分区的基本使用
  • 6月5号作业
  • 中继器、集线器、网桥、交换机、路由器和网关
  • 揭秘相似矩阵:机器学习算法中的隐形“纽带”
  • 攻防世界—webbaby详解
  • MySQL中:cmd下输入命令mysql -uroot -p 连接数据库错误
  • 【开发利器】使用OpenCV算子工作流高效开发
  • 基础数学-求平方根(easy)
  • c语言项目-贪吃蛇项目2-游戏的设计与分析
  • 力扣2831.找出最长等值子数组
  • 17K star,一款开源免费的手机电脑无缝同屏软件
  • 正则表达式二
  • 我的创作纪念日--我和CSDN一起走过的1825天
  • 递归书写树形图示例
  • 【python】IndexError: Replacement index 1 out of range for positional args tuple
  • Spring自带定时任务@Scheduled注解
  • 代码随想录算法训练营第二十九天|LeetCode491 非递减子序列、LeetCode46 全排列、LeetCode47 全排列Ⅱ