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

时间:2017-08-02 05:44:13

标签: nginx openresty

我正在使用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;
    }

这会导致浏览器询问用户凭据,但现在如何从客户端获取/处理用户凭据?

2 个答案:

答案 0 :(得分:0)

ngx_http_auth_request_module根据子请求的结果实现客户端授权。

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

答案 1 :(得分:-1)

在这里回答问题:Nginx authentication with auth_request module 我可以通过访问我的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;
}

以下conf允许我验证其凭据存储在restrict.lua文件中的redisDB中的用户,该文件返回200或401代码,具体取决于用户的凭据返回/位置块。

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