【问题标题】:Calling resource server with JWT returns HTTP 403 by way of Spring Cloud Gateway使用 JWT 调用资源服务器通过 Spring Cloud Gateway 返回 HTTP 403
【发布时间】:2021-04-03 03:56:08
【问题描述】:

我有两个项目。两者都是反应式 Spring。项目一是 Javascript 应用程序和 Spring Cloud Gateway 的组合,用于反向代理。第二个项目是 Spring 资源服务器。

将来自/api/artists 的代理请求投射到http://localhost:8081/v1/artists 的第二个。

如果我使用有效的 JWT 直接调用资源服务器(项目二),则响应返回 HTTP 200。如果我通过项目一中的反向代理,并使用相同的 JWT 访问 http://localhost:8080/api/artists,我从项目 2 接收 HTTP 403,并通过项目 1 传播回来。

这是我的 Spring Cloud Gateway 配置:

spring:
  cloud:
    gateway:
      routes:
        - id: experience-api
          uri: http://localhost:8081/v1/artists
          predicates:
            - Path=/api/artists/**
          filters:
            - TokenRelay=

HTTP 403 表示虽然令牌有效,但它必须缺少执行操作的其他权限。不过,我不确定为什么直接调用它而不是通过反向代理/Spring Cloud Gateway 调用它。

【问题讨论】:

    标签: spring spring-boot spring-security spring-cloud spring-cloud-gateway


    【解决方案1】:

    离开几天后,我意识到我的网关配置不正确。我意识到原来的配置是将请求代理到/v1/artists/api/artists,这在项目二中不存在,但是我的安全配置设置为/v1/** 需要身份验证。我怀疑这就是为什么我在看到 HTTP 404 Not Found 之前看到了 HTTP 403 Forbidden。

    我最终使用了以下配置:

    spring:
      cloud:
        gateway:
          routes:
            - id: experience-api
              uri: http://localhost:8081
              predicates:
                - Path=/v1/artists/**
              filters:
                - TokenRelay=
    

    请注意,我从 uri 属性中删除了 /v1/artists。现在,http://localhost:8080/v1/artists 的项目请求正在代理到http://localhost:8081/v1/artists。我本可以使用 StripToken 谓词过滤器,但它不如这个干净。

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 2019-12-23
      • 2021-10-23
      • 2018-07-19
      • 2021-12-08
      • 2021-06-27
      • 1970-01-01
      • 2021-07-26
      • 2018-04-23
      相关资源
      最近更新 更多