You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替代TokenRelayGatewayFilterFactory的微服务令牌转发API是什么?

Spring网关JWT令牌转发:替代弃用的TokenRelayGatewayFilterFactory

1. 依赖调整

移除原有的spring-cloud-security依赖,改用Spring Cloud Gateway核心依赖及对应OAuth2组件:

<!-- Spring Cloud Gateway 核心依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!-- 根据OAuth2场景选择:客户端模式选这个 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

<!-- 或者资源服务器模式选这个 -->
<!-- <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency> -->

2. 代码替换

无需再注入TokenRelayGatewayFilterFactory,直接使用Gateway内置的tokenRelay()过滤器方法即可:

public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
            .route("car-service", r -> r.path("/cars")
                    .filters(f -> f.tokenRelay())
                    .uri("lb://car-service"))
            .build();
}

说明

tokenRelay()是Spring Cloud Gateway提供的内置过滤器,会自动从当前请求的认证上下文提取JWT令牌,并将其注入到转发至后端微服务的请求头中(默认使用Authorization头),完全替代了原废弃API的功能。

内容的提问来源于stack exchange,提问作者Katlock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:05:21