如何在Envoy的envoy_on_request中为httpCall添加请求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
相关产品推荐
相关产品推荐

