Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 如何使用lua代码重定向到EnvoyFilter中的登录页面?_Kubernetes_Lua_Istio_Envoyproxy - Fatal编程技术网

Kubernetes 如何使用lua代码重定向到EnvoyFilter中的登录页面?

Kubernetes 如何使用lua代码重定向到EnvoyFilter中的登录页面?,kubernetes,lua,istio,envoyproxy,Kubernetes,Lua,Istio,Envoyproxy,我是Lua的新手,下面是我的“EnvoyFilter”内联代码。我花了几个小时搜索了一些文章和帖子。但是,没有找到任何合适的答案,因此发布了这个问题 这个场景是当我得到一个503错误代码时,我想重定向到登录页面 function envoy_on_response(response_handle) if response_handle:headers():get(":status") == "503"

我是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
响应来指示用户浏览器向登录页面发出新请求

             function envoy_on_response(response_handle)
                if response_handle:headers():get(":status") == "503" then
                  #TODO Redirect to http://login-page.com
                end
              end
像这样的方法应该会奏效:

首先添加一些日志来验证它是否正常工作。 现在替换标题中的
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
更多灵感请参见文档:

说到这里,我想补充一点,这可能不是处理故障的好方法。用户不会被告知错误,只会被重定向并登录到登录页面,而不知道原因


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

谢谢你,克里斯托夫,我也这么做了。但是,面对response_handle:headers():replace(“status”,“302”)的问题,这并不是在替换旧的状态。我可以在浏览器响应中看到2个不同的状态代码。是否有替代方法来替换状态?为了让用户知道错误,我们计划显示一个中间弹出类型。首先尝试删除状态标题,然后添加一个新标题:
response\u handle:headers():remove(“status”)
,然后再添加(“status”,“302”)。此外,键可能是:status``而不是status。关于弹出窗口:为什么不让前端处理重定向?我试图删除它,但它正在删除我新添加的状态而不是旧的状态。甚至试图删除“:status”,它也会因错误而崩溃。于是,他又回来了。。不确定如何删除503原始错误。最后这个替换代码成功了:)响应\句柄:headers():replace(“:status”,“307”)