kubernetes - 如何使用 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
解决方案
您可以使用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
话虽如此,我想补充一点,这可能不是故障处理的好方法。用户不会被告知错误,只会被重定向并登陆登录页面,而不知道为什么。
你想解决什么问题?也许有更好的方法。
推荐阅读
- reactjs - 如何将数据从反应应用程序发送到本地休息 api
- java - Eclipse 项目无法运行
- google-people-api - 如何在 Google People API 上使用同步令牌
- node.js - AWS Lambda 错误 - “errorType”:“字符串”、“errorMessage”:“已处理”
- mongodb - MongoDB中的ISODate没有时间
- haskell - 将列表添加为另一个列表中的元素
- c# - 如何使用 Select 2 和 Json 显示大数据集
- java - Java应用程序:检测是否从挂载的dmg启动
- sqlite - 跨列而不是跨行选择最大值
- javascript - 如何使用附加数据更新现有对象