【问题标题】:Transcode HTTP header into Grpc metadata for each request将每个请求的 HTTP 标头转码为 Grpc 元数据
【发布时间】:2022-11-03 05:40:45
【问题描述】:

我正在构建一个将 HTTP 流量代理到 Grpc 服务的 API 网关。所有传入的 HTTP 请求都可以在 Authorization 标头中包含 JWT。我需要在每次请求时将此 JWT 转码为 Grpc 元数据,并与 Grpc 请求一起发送。我正在使用带有 grpc 代码生成器的 grpc-kotlin 库,用于客户端存根的 kotlin 挂起函数。 我已经编写了这个 WebFilter 来将标头放入 ReactorContext:

@Component
class UserMetadataWebFilter : WebFilter {

  override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
    exchange.request.headers[HttpHeaders.AUTHORIZATION]?.firstOrNull()?.let { authorizationHeader ->
        return chain.filter(exchange).contextWrite { Context.of("myHeader", authorizationHeader) }
    }
    return chain.filter(exchange)
  }
}

它可以在控制器方法中使用,如下所示:

identityProviderClient.createUser(protobufRequest,
            coroutineContext[ReactorContext]?.context?.get("myHeader") ?: Metadata())

我想创建 Grpc 客户端拦截器或其他东西来从协程上下文中自动设置 Grpc 元数据。我有很多 Grpc 客户端调用,我认为为每个调用编写此代码并不是一个好习惯。 我知道 envoy-proxy,但我需要对我的请求应用特定的逻辑,这就是为什么 envoy-proxy 不是我的选择。 我应该如何将 Http 标头转码为 grpc 客户端调用元数据?谢谢。

【问题讨论】:

    标签: spring-boot kotlin grpc grpc-kotlin


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2021-09-05
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    相关资源
    最近更新 更多