首页 > 解决方案 > 带有角度路由的 Spring Cloud Gateway

问题描述

我有一个 Spring Cloud Gatway,它路由到一个 Angular 应用程序,该应用程序托管在 localhost:9092/admin 下的 Tomcat 服务器上。这是我的网关配置

server:
  port: 9091
spring:
  cloud:
    gateway:
      routes:
      - id: adminui
        uri: http://localhost:9092
        predicates:
        - Path=/admin/**

这运作良好。现在我开始使用 Angular 路由并添加了子 URL,例如

/admin/users

在我在浏览器中点击刷新之前,或者当我尝试直接导航到给定的 url 之前,这一直很好。然后我得到一个

404-NotFound 

来自网关,因为在托管 Angular“dist”的 Tomcat 文件夹中找不到此资源。

标签: angularspring-cloud-gateway

解决方案


实际上这是一个普遍的问题,在 Tomcat 上托管这样的 SPA 页面。因此,当您将 dist 文件夹部署到 tomcat 服务器上时,与此处的 angular2 路由不起作用的问题相同

解决方案是告诉网关将请求路由到 Tomcat 中 index.html 的任何 Angular 路由

  routes:
  - id: adminuisub
    uri: http://localhost:9092
    predicates:
    - Path=/admin/users/**
    filters:
    - SetPath=/admin/index.html

推荐阅读