如何使用 lua 代码重定向到 EnvoyFilter 中的登录页面?
How to redirect to login page in EnvoyFilter using lua code?
我是 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
您可以使用 302 Found
响应来指示用户浏览器向登录页面发出新请求。
像这样的东西应该可以工作:
首先添加一些日志记录来验证它是否正常工作。
现在替换 header 中的 status
字段并将其设置为 302
。
最后向 header 添加一个 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
话虽如此,我想补充一点,这可能不是处理故障的好方法。用户不会被告知错误,而只是被重定向并登陆登录页面,不知道为什么。
您要解决什么问题?也许有更好的方法。
我是 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
您可以使用 302 Found
响应来指示用户浏览器向登录页面发出新请求。
像这样的东西应该可以工作:
首先添加一些日志记录来验证它是否正常工作。
现在替换 header 中的 status
字段并将其设置为 302
。
最后向 header 添加一个 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
话虽如此,我想补充一点,这可能不是处理故障的好方法。用户不会被告知错误,而只是被重定向并登陆登录页面,不知道为什么。
您要解决什么问题?也许有更好的方法。