如何在nginx中处理来自www-authenticate的响应?

我正在使用openresty nginx v1.11.2.4。我希望能够在用户访问资源或在他们尝试在服务器上放置某些内容之前对他们进行身份验证。我正在使用http_auth_request_module,以下是我的nginx.conf文件的摘录:

location /video/ {
        auth_request /auth;
        root /usr/local/openresty/nginx/html;
    }

    location = /auth {
        more_set_headers "WWW-Authenticate: Basic";
        return 401;
    }

这会导致浏览器要求用户凭据,但现在我该如何获取/处理来自客户端的用户凭据?

点赞
用户2060502
用户2060502

说明: ngx_http_auth_request_module 实现了基于子请求结果的客户端授权。

如果要使用基本身份认证,不需要使用 ngx_http_auth_request_module。请使用 http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

2017-08-03 09:14:23
用户4542614
用户4542614

根据这个问题的答案:使用 auth_request 模块在 Nginx 中进行身份验证,我可以通过访问 nginx.conf 文件中的 $http_authorization 变量来处理用户名和密码。以下是来自我的 nginx.conf 的一部分:

location /video {
        satisfy any;
        auth_basic "Private Property";
        auth_basic_user_file /usr/local/openresty/nginx/conf/htpasswd;
        auth_request /auth;
        root /usr/local/openresty/nginx/html;
        client_max_body_size 1000M;
        if ($request_method != "GET"){
            content_by_lua_file /root/Documents/contentbylua.lua;
        }
    }

location = /auth {
    set $authHeader $http_authorization;
    set $authUName $remote_user;
    content_by_lua_file /root/Documents/restrict.lua;
}

以上的配置允许我验证一个存储在 restrict.lua 文件中 redisDB 中的用户凭据,并将 200 或 401 状态码返回到 /location 块。

在 restrict.lua 文件中,通过 ngx.var.authHeader 访问响应(用户名和密码)。进行一些字符串处理以删除“Basic”,然后对其余部分进行 base64 解码,最后对其进行一些字符串处理以获取密码。就是这样。

2017-08-08 07:52:26