Unable to find GatewayFilterFactory with name TokenRelay

目录

问题分析

Spring Cloud Gateway 网关作为代理资源服务器,需要将 JWT 传递给下游资源服务器,下面是网关的配置

yaml 复制代码
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 启用服务发现
          lower-case-service-id: true

      default-filters:
      	# 中继令牌
        - TokenRelay
        # 移除Cookie请求头,避免cookie透传到后端服务
        - RemoveRequestHeader=Cookie

TokenRelay 激活 TokenRelayGatewayFilterFactory,将令牌中继传递给下游资源服务,例如系统服务 (youlai-system)

但是项目启动中会报错:

复制代码
Unable to find GatewayFilterFactory with name TokenRelay

解决方案

参考链接:Spring Gateway and Auth0: IllegalArgumentException: Unable to find GatewayFilterFactory with name TokenRelay

TokenRelay 过滤器的工作原理是,当一个请求到达网关时,它会从请求中提取 JWT,然后将其添加到转发到资源服务器的请求头中。这样,资源服务器就可以从请求头中提取 JWT,并使用它来验证用户的身份和权限。

spring-boot-starter-oauth2-client 依赖包提供了 OAuth2 客户端的功能,包括处理和转发 JWT。在 Spring Cloud Gateway 中,它可以与 TokenRelay 过滤器一起工作,安全地处理和转发 JWT。

所以添加 oauth2-client 依赖即可

xml 复制代码
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-oauth2-client</artifactId>
 </dependency>

参考文档

Spring Cloud Gateway 官方文档: The TokenRelay GatewayFilter Factory

开源项目

微服务商城项目

Github Gitee
开源组织 有来开源组织 有来开源组织
后端 youlai-mall 📖 youlai-mall 📖
前端 mall-admin🌎 mall-admin 🌎
移动端 mall-app 🌎 mall-app 🌎

前后端分离项目

Github Gitee
开源组织 有来开源组织 有来开源组织
后端 youlai-boot 📖 youlai-boot 📖
前端 vue3-element-admin 🌎 vue3-element-admin 🌎
相关推荐
萧曵 丶5 分钟前
Spring Cloud Alibaba 详解
spring·spring cloud
潞哥的博客8 小时前
Ingress nginx退役,该怎么换,gateway api 上线
运维·gateway·k8s
廋到被风吹走20 小时前
【Spring】Spring Cloud 分布式事务:Seata AT/TCC/Saga 模式选型指南
分布式·spring·spring cloud
刘一说1 天前
Spring Cloud微服务中的分布式追踪:从故障定位到性能优化的革命性实践
分布式·spring cloud·微服务
计算机程序设计小李同学1 天前
平价药店销售与管理系统
java·mysql·spring·spring cloud·ssm
廋到被风吹走1 天前
【Spring】Spring Cloud 链路追踪:SkyWalking/Pinpoint 字节码增强与 TraceId 传递机制
spring·spring cloud·skywalking
自燃人~1 天前
DiscoveryClient 和 NamingService
spring·spring cloud·nacos
u0104058361 天前
Spring Boot与Spring Cloud的协同:构建健壮的微服务架构
spring boot·spring cloud·架构
用户2190326527351 天前
SpringCloud分布式追踪深度实战:Sleuth+Zipkin从入门到生产部署全攻略
分布式·后端·spring cloud
南山十一少1 天前
最新款2025版的IDEA的下载、注册以及进行spring boot 工程和spring cloud工程的搭建和使用
spring boot·spring cloud·intellij-idea