我正在尝试将 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/