首页 > 解决方案 > 如何使用 lua 代码重定向到 EnvoyFilter 中的登录页面?

问题描述

我是 Lua 新手,下面是我的“EnvoyFilter”内联代码。我花了几个小时搜索一些文章和帖子。但是,没有找到任何合适的答案,因此发布了这个问题。

这种情况是当我收到一个 503 错误代码时,我想重定向到登录页面。

             function envoy_on_response(response_handle)
                if response_handle:headers():get(":status") == "503" then
                  #TODO Redirect to http://login-page.com
                end
              end

任何帮助或建议都会有所帮助和赞赏。

[工作答案]

function envoy_on_response(response_handle)
  if response_handle:headers():get(":status") == "503" then                
    response_handle:headers():replace("content-type", "text/html")                
    response_handle:headers():replace(":status", "307")                
    response_handle:headers():add("location", "https://login-page.com")
  end
end 

标签: kubernetesluaistioenvoyproxy

解决方案


您可以使用302 Found响应来指示用户浏览器向登录页面发出新请求。

像这样的东西应该工作:

首先添加一些日志来验证它是否正常工作。现在替换status标题中的字段并将其设置为302. 最后location在标题中添加一个字段,其中包含您要重定向到的 url。

function envoy_on_response(response_handle)
    if response_handle:headers():get(":status") == "503" then
        response_handle:logInfo("Got status 503, redirect to login...")
        response_handle:headers():replace(":status", "302")
        response_handle:headers():add("location", "http://login-page.com")
    end
end

请参阅文档以获取更多灵感:https ://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter

话虽如此,我想补充一点,这可能不是故障处理的好方法。用户不会被告知错误,只会被重定向并登陆登录页面,而不知道为什么。

你想解决什么问题?也许有更好的方法。


推荐阅读