当前位置:   article > 正文

网关(Gateway)- 自定义过滤器工厂

网关(Gateway)- 自定义过滤器工厂

自定义过滤工厂类

DemoGatewayFilterFactory

  1. package com.learning.springcloud.custom;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.springframework.cloud.gateway.filter.GatewayFilter;
  4. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  5. import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.server.ServerWebExchange;
  9. import reactor.core.publisher.Mono;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. @Component
  13. public class DemoGatewayFilterFactory extends AbstractGatewayFilterFactory<DemoGatewayFilterFactory.Config> {
  14. public DemoGatewayFilterFactory() {
  15. super(Config.class);
  16. }
  17. public List<String> shortcutFieldOrder() {
  18. return Arrays.asList("name", "value");
  19. }
  20. @Override
  21. public GatewayFilter apply(DemoGatewayFilterFactory.Config config) {
  22. return new GatewayFilter() {
  23. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  24. String value = exchange.getRequest().getQueryParams().getFirst("name");
  25. if (StringUtils.isBlank(value)) {
  26. // 为空 则证明是正常的请求 需要放行
  27. return chain.filter(exchange);
  28. }
  29. if ("YES".equals(value)) {
  30. return chain.filter(exchange);
  31. }
  32. exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
  33. return exchange.getResponse().setComplete();
  34. }
  35. };
  36. }
  37. public static class Config {
  38. private String name;
  39. private String value;
  40. public String getName() {
  41. return name;
  42. }
  43. public void setName(String name) {
  44. this.name = name;
  45. }
  46. public String getValue() {
  47. return value;
  48. }
  49. public void setValue(String name) {
  50. this.value = name;
  51. }
  52. }
  53. }

过滤器配置说明

  1. server:
  2. port: 8088
  3. spring:
  4. application:
  5. name: api-gateway
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8847
  10. username: nacos
  11. password: nacos
  12. gateway:
  13. routes:
  14. - id: order_route # 路由唯一标识
  15. #uri: http://localhost:8020 # 需要转发的地址
  16. uri: lb://order-service # 需要转发的地址
  17. # 断言规则 用于路由规则的匹配
  18. predicates:
  19. # - Path=/order-serv/**
  20. # - After=2024-01-01T23:00:11.518+08:00[Asia/Shanghai]
  21. # - Before=2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
  22. - Between=2024-01-01T23:00:11.518+08:00[Asia/Shanghai],2025-01-01T23:00:11.518+08:00[Asia/Shanghai]
  23. # - Header=X-Request-Id,\d+
  24. # - Host=127.0.0.1
  25. # - Query=name,lq
  26. # - Method=GET,POST
  27. # http://localhost:8088/order-serv/order/add => http://localhost:8020/order-serv/order/add
  28. - Demo=YES
  29. #配置过滤器工厂
  30. filters:
  31. - StripPrefix=1 # 转发去掉第一层路径
  32. - AddRequestHeader=X-Request-name,tom #添加请求头
  33. - AddRequestParameter=color, blue # 添加请求参数
  34. - PrefixPath=/demo
  35. # - RedirectTo=302, https://www.baidu.com/ #重定向到百度
  36. - Demo=name,YES # 自定义过滤工厂配置
  37. # http://localhost:8020/order-serv/order/add => http://localhost:8020/order/add

访问效果

  • 不带name参数的请求放行

  • 带name参数值不是YES的拦截,返回 not found

  • 带name参数值是YES的放行

实现说明 

  • 命名必须需要以 FilterFactory 结尾(约定规范)
  • 继承 AbstractGatewayFilterFactory 类
  • 必须为spring的组件bean(@Component)
  • 必须要有内部类 Config 以及 对应的 shortcutFieldOrder 方法
  • 重写 apply 方法的逻辑 (apply(DemoGatewayFilterFactory.Config config))
  • 可通过 exchange.getRequest() 获取到 ServerHttpRequest 对象
  • 从而获取到请求的参数、请求方式、请求头等信息
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/667806
推荐阅读
相关标签
  

闽ICP备14008679号