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

如何在Envoy的envoy_on_request中为httpCall添加请求Cookie

Envoy路由配置:将原始请求Cookie传递至认证服务

需求:在请求转发至backend集群前,先向auth-service发起HTTP调用,且需携带原始请求中的Cookie信息。

当前配置的Lua过滤器未实现Cookie传递逻辑,以下是修正后的完整路由配置:

- match:
    prefix: /
    case_sensitive: false
  route:
    cluster: backend 
  typed_per_filter_config:
    envoy.filters.http.ext_authz:
      "@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthzPerRoute
      disabled: true
    envoy.filters.http.lua:
      "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.LuaPerRoute
      source_code:
        inline_string: |
          function envoy_on_request(request_handle)
            -- 获取原始请求中的Cookie头
            local cookie = request_handle:headers():get("Cookie")
            -- 构造认证服务请求的headers,若存在Cookie则添加
            local auth_headers = {
              [":method"] = "GET",
              [":path"] = "/",
              [":authority"] = "auth-service",
            }
            if cookie ~= nil then
              auth_headers["Cookie"] = cookie
            end
            -- 发起对认证服务的调用
            local headers, body = request_handle:httpCall(
                "auth-service",
                auth_headers,
                "",
                50)
          end

关键修改说明

  • 新增逻辑从原始请求中提取Cookie头信息
  • 在构造认证服务请求的headers时,判断Cookie是否存在,避免传递空值
  • 将提取到的Cookie添加至认证服务的请求头中,确保认证服务能获取到原始请求的Cookie数据

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 22:47:40