当前位置: 代码网 > it编程>编程语言>Java > springCloud Gateway StripPrefix和PrefixPath过滤器的区别及说明

springCloud Gateway StripPrefix和PrefixPath过滤器的区别及说明

2025年06月23日 Java 我要评论
一、 stripprefix filterstripprefix filter 是一个请求路径截取的功能。server: port: 8080spring: application: nam

一、 stripprefix filter

stripprefix filter 是一个请求路径截取的功能。

server:
  port: 8080
spring:
  application:
    name: user
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      - id: user
        uri: lb://user
        #uri: http://localhost:8080
        predicates:
        - path=/zuul/api/user/**
        filters:
        - stripprefix=3

主要看这里

- path=/zuul/api/user/**
filters:
- stripprefix=3 

当请求路径匹配到/zuul/api/user/**会将包含zuul和后边的字符串接去掉转发,stripprefix=3就代表截取路径的个数

这样配置后当请求/zuul/api/user/aaa后端匹配到的请求路径,就会变成http://localhost:8080/aaa

二、prefixpath filter

prefixpath filter 的作用和 stripprefix 正相反,是在 url 路径前面添加一部分的前缀。

server:
  port: 8080
 
spring:
  application:
    name: user
  cloud:

    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      - id: user
        uri: lb://user
        predicates:
        - path=/**
        filters:    
        - prefixpath=/hi

主要看这里

 predicates:
        - path=/**
        filters:    
        # 当访问 http://localhost:8080/aaa,加上前缀就变成 http://localhost:8080/hi/aaa
        - prefixpath=/hi

当访问 http://localhost:8080/aaa,加上前缀就变成 http://localhost:8080/hi/aaa

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com