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 🌎
相关推荐
小猪咪piggy1 天前
【微服务】(1) Spring Cloud 概述
java·spring cloud·微服务
choice of1 天前
Sentinel:阿里云高并发流量控制
笔记·spring cloud·sentinel
xrkhy2 天前
微服务之SpringCloud Alibaba(注册中心Nacos)
spring cloud·微服务·架构
唐僧洗头爱飘柔95272 天前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
鹿里噜哩2 天前
Nacos跨Group及Namespace发现服务
java·spring cloud
xrkhy2 天前
微服务之Gateway网关(1)
微服务·架构·gateway
楚韵天工2 天前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
linweidong2 天前
理想汽车Java后台开发面试题及参考答案(下)
jvm·spring boot·spring cloud·rpc·虚拟机·feign·二叉树排序
洛克大航海3 天前
3-SpringCloud-LoadBalancer-OpenFeign服务调用与负载均衡
spring·spring cloud·负载均衡·openfeign·loadbalancer
W.Buffer4 天前
SpringCloud-Sentinel实战与源码分析:从流量防护到底层实现
spring·spring cloud·sentinel