NGINX Lua 基础 HTTP 授权/认证 允许一定数量的尝试次数

我正在尝试使用我在这个 Git 存储库中找到的 Lua 脚本:https://github.com/knq/nginx-crowd-lua/blob/master/crowd-auth.lua 对 Atlassian Crowd 用户管理系统的用户进行授权,以访问受限内容。

使用原始脚本时发生的情况是,如果用户输入错误的用户名/密码,则浏览器返回 403 Forbidden 响应,不允许用户再次尝试,除非用户关闭浏览器并返回页面。

我正在尝试允许 3 次尝试登录后显示 403 页面。

我已尝试循环此脚本,但是循环不起作用,一个错误的用户/密码会直接在第一次尝试时跳转到 403 页面,或者循环是无限的,允许无限次尝试。

请有人能提供建议吗? 如果您需要更多详细信息,请评论并我将提供。

谢谢

点赞
用户2300070
用户2300070

我已经找到解决方法。对于那些也对此感兴趣的人,我已经发布了解决方案:

ngx.header['WWW-Authenticate'] 命令停止脚本并发送一个新请求。无论在 NGINX 还是 Lua 中的任何类型的变量都无法跨请求保持它的值。您可以做的事情是设置一个保存最大身份验证尝试次数的 cookie,如下所示:

local auth_tries = ngx.var.cookie_AuthAttempts

if not auth_tries then
  auth_tries = 1
end

if type(auth_tries) == "string" then
  auth_tries = tonumber(auth_tries)
end

然后在循环结束时设置计数器,并将 cookie 设置为其值:

auth_header = nil
auth_tries = auth_tries + 1

ngx.header['Set-Cookie'] = "AuthAttempts="..auth_tries.."; Path=/; Expires="..ngx.cookie_time(ngx.time() + 3600*24)

然后,如果已达到最大失败尝试次数,请清除数据并发送 401 响应:

ngx.header['Set-Cookie'] = "AuthAttempts=0; Path=/; Expires="..ngx.cookie_time(ngx.time() + 3600*24)

if res ~= nil then
if res.status ~= 200 then
  ngx.exit(ngx.HTTP_FORBIDDEN)
end
else
  ngx.exit(ngx.HTTP_FORBIDDEN)
end
2016-02-22 06:21:58
用户2060502
用户2060502

使用 Fail2Ban。 以下是一个使用方法示例: https://www.digitalocean.com/community/tutorials/how-to-protect-an-nginx-server-with-fail2ban-on-ubuntu-14-04

上述解决方案无法保护免受 DDoS 攻击。

2016-02-23 16:21:10