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

官方文档:Spring Cloud Gateway

内置过滤器工厂

AddRequestHeaderGatewayFilterFactory

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

配置说明

java 复制代码
server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8847
        username: nacos
        password: nacos
    gateway:
      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

代码编写

java 复制代码
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

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

配置说明

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

代码编写

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

访问效果

PrefixPathGatewayFilterFactory

增加项目路径的过滤器

配置说明

java 复制代码
# order-service 服务配置
server:
  port: 8020
  servlet:
    context-path: /demo

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

访问效果

  • 不携带 配置的项目路径 demo 是无法访问的
  • 携带配置的项目路径demo是可以访问的
  • 通过网关访问不携带项目路径demo 可以正常访问

RedirectToGatewayFilterFactory

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

配置说明

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

访问效果

相关推荐
晏宁科技YaningAI1 天前
全球短信路由系统设计逻辑打破 80%送达率瓶颈:工程实践拆解
网络·网络协议·架构·gateway·信息与通信·paas
ꟼ ꟼ✚19226382 天前
基于 FPGA 的 16QAM 调制解调系统功能说明文档
gateway
奥升新能源平台2 天前
奥升充电最小化高可用机房部署方案
运维·安全·开源·能源·springcloud
code_pgf3 天前
openclaw配置高德导航、京东商品搜索、QQ 音乐播放控制
人工智能·gateway·边缘计算
码克疯v13 天前
OpenClaw 安装与入门:从零到跑通 Gateway(详细可操作)
gateway·openclaw·龙虾
qqty12174 天前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
北巷`4 天前
OpenClaw内部原理完全解析:从Gateway到记忆系统的AI Agent基础设施
人工智能·gateway
yuweiade4 天前
SpringGateway网关(Spring Gateway是Spring自己编写的,也是SpringCloud中的组件)
spring·spring cloud·gateway
yoyo_zzm4 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
大灰狼来喽5 天前
OpenClaw 多平台接入全指南:Telegram + Discord + 飞书 + WhatsApp,一个 Gateway 管所有
gateway·飞书