Spring 云网关和 TokenRelay 过滤器

Spring Cloud Gateway and TokenRelay Filter

我正在尝试将 JHipster 从使用 Zuul 迁移到 Spring Cloud Gateway。 JHipster 使用 Eureka 查找路由,我相信我已经正确配置 Spring Cloud Gateway 来查找路由并将访问令牌传播给它们。这是我的配置:

spring:
  cloud:
    gateway:
      default-filters:
        - TokenRelay
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
          route-id-prefix: /services/
      httpclient:
        pool:
          max-connections: 1000

我遇到的问题是访问令牌没有向下游服务发送 Authorization header。

下面是我的 application.yml 中使用 Zuul 进行配置的方式:

zuul: # those values must be configured depending on the application specific needs
  sensitive-headers: Cookie,Set-Cookie #see https://github.com/spring-cloud/spring-cloud-netflix/issues/3126
  host:
    max-total-connections: 1000
    max-per-route-connections: 100
  prefix: /services
  semaphore:
    max-semaphores: 500

我创建了一个拉取请求以显示集成 Spring 云网关后发生的变化。

https://github.com/mraible/jhipster-reactive-microservices-oauth2/pull/4

重现问题的步骤:

git clone -b reactive git@github.com:mraible/jhipster-reactive-microservices-oauth2.git

启动 JHipster Registry、Keycloak 和网关应用程序:

cd jhipster-reactive-microservices-oauth2/gateway
docker-compose -f src/main/docker/jhipster-registry.yml up -d
docker-compose -f src/main/docker/keycloak.yml up -d
./mvnw

启动 MongoDB 和博客应用程序:

cd ../blog
docker-compose -f src/main/docker/mongodb.yml up -d
./mvnw

在浏览器中导航到 http://localhost:8080,使用 admin/admin 登录,然后尝试转到 Entities > Blog。您将收到 403 拒绝访问错误。如果您在 Chrome 开发人员工具中查看网络流量,您会发现访问令牌未包含在任何 header 中。

我使用 解决了这个问题。

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          predicates:
            - name: Path
              args:
                pattern: "'/services/'+serviceId.toLowerCase()+'/**'"
          filters:
            - name: RewritePath
              args:
                regexp: "'/services/' + serviceId.toLowerCase() + '/(?<remaining>.*)'"
                replacement: "'/${remaining}'"

我还必须将 .pathMatchers("/services/**").authenticated() 添加到我的安全配置中,Zuul 不需要。你可以看到我的commit here