spring-boot - Spring Cloud Gateway 和 TokenRelay 过滤器

标签 spring-boot spring-security jhipster spring-security-oauth2 spring-cloud-gateway

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

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

我遇到的问题是访问 token 没有发送 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 Cloud Gateway 后发生了什么变化。

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 登录,然后尝试转到 实体 > 博客 .您将收到 403 访问被拒绝错误。如果您在 Chrome 开发者工具中查看网络流量,您会看到访问 token 未包含在任何 header 中。

最佳答案

我能够使用 this answer 解决这个问题.

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 .

关于spring-boot - Spring Cloud Gateway 和 TokenRelay 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251863/

相关文章:

spring-boot - @AllArgsConstructor 和构造函数注入(inject) Spring : is private final needed?

java - 参数值 [1] 与预期类型不匹配

grails - Grails Spring Security Switch用户设置问题

java - 如何通过类实现 Autowiring SecurityContextRepository

grails - Spring 安全核心插件 - 如何从 Controller 访问用户 ID

java - 由于未找到 Node 命令,Git 钩子(Hook)预提交中的提交失败

JHipster 应用程序的 node.js REST 客户端

java - 根据spring profile导入bean资源

java - 实现Spring Service根据配置向不同的Kafka主题发送消息

java - 更新 JHipster 后不再提供 Angular 组件,仅提供 root index.html