【问题标题】:Spring Cloud Gateway to modify HREF static HTMLSpring Cloud Gateway 修改 HREF 静态 HTML
【发布时间】:2020-01-21 07:39:22
【问题描述】:

我有一个 Spring Gateway 和一个 Webflux 服务。路线是;

server:
  port: 9999
spring:
  application:
    name: discovery-service
  cloud:
    gateway:
      routes:
      - id: route1
        predicates:
          - Path=/1/**
        uri: http://localhost:8081
        filters:
          - RewritePath=/1/(?<myPath>.*), /$\{myPath}

如果我请求localhost:9999/1/index.html,它会转换为localhost:8081/index.html,并且页面会正确返回。

但是,在 HTML 中,我有链接和参考,例如

<a href="/microService1">test1</a>

如何让网关返回带有更正 HREF 的 HTML? (即返回 HTML 为);

<a href="/1/microService1">test1</a>

附带说明;在包含微服务的 Webflux/Netty 服务器中托管静态 HTML、CSS 和 JS 是常见的还是公认的做法,还是应该将它们放在其他地方?

【问题讨论】:

    标签: spring spring-boot spring-webflux api-gateway


    【解决方案1】:

    你只需要删除你的href中的第一个/

    <a href="microService1">test1</a>
    

    或者添加一个 .

    <a href="./microService1">test1</a>
    

    【讨论】:

    • 我走进了 Spring Gateway、RouteLocators 和 modifyResponseBody 的兔子洞。我从来没有考虑过使用相对地址而不是绝对地址!该解决方案有效。谢谢哈立德!
    猜你喜欢
    • 2020-10-12
    • 2020-07-19
    • 1970-01-01
    • 2019-03-31
    • 2018-07-06
    • 2021-07-23
    • 1970-01-01
    • 2021-10-20
    • 2021-01-23
    相关资源
    最近更新 更多