django - Nginx密码认证不断提示输入密码

标签 django authentication nginx uwsgi

我想上传我网站的开发分支,以便我可以向客户展示它并在尽可能接近生产的环境中进行测试(使用可能尚未准备好用于生产的代码)。因此我想用密码保护这个网站。

我正在使用 Django 开发一个网站,并使用 nginx 为该网站提供服务(使用 uWsgi)。我设法收到应用以下指令的密码提示:

auth_basic "Restricted Content";  # also tried "Private Property"
auth_basic_user_file /etc/nginx/.htpasswd;

但问题是,正确输入第一个密码后,它一直提示我再次输入用户名和密码;就好像每个 API 调用都需要进行身份验证。

我认为问题可能出在我的配置文件上,所以这是我的 site.conf 文件:

server {
    listen 80;
    server_name panel.mysite.dev;
    root /path/to/my/app/front/dist;

    ### I've also tried 'auth_basic' here

    location / {

        root /path/to/my/app/front/dist;
        index index.html;

        auth_basic "Private Property";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
    location /media {
        rewrite ^(.*)$ http://media.mysite.dev$1;
    }
    location /static {
        rewrite ^(.*)$ http://static.mysite.dev$1;
    }

}

server {
    listen 80;
    server_name api.mysite.dev;

    ### I've also tried 'auth_basic' here

    location /api {
        client_max_body_size 25m;
        uwsgi_pass unix:/tmp/api.mysite.dev.sock;
        include /path/to/my/app/back/uwsgi_params;
    }

}
server {
    listen 80;
    server_name media.mysite.dev;
    root /path/to/my/app/media;
    add_header 'Access-Control-Allow-Origin' '.*\.mysite\.[com|dev]';

    location / {
        root /path/to/my/app/media;
    }
}
server {
    listen 80;
    server_name static.mysite.dev;
    root /path/to/my/app/static;
    if ($http_origin ~* (https?://.*\.mysite\.[com|dev](:[0-9]+)?)) {
        set $cors "true";
    }
    location / {
        if ($cors = "true") {
            add_header 'Access-Control-Allow-Origin' "$http_origin";
        }
    }
}

我的问题:有什么方法可以记住输入的密码并允许经过身份验证的用户轻松导航吗?或者我错过了一些微不足道的事情?

编辑: 在我的 django settings.py 中:

AUTHENTICATION_BACKENDS = (
    'oauth2_provider.backends.OAuth2Backend',
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)
...
REST_FRAMEWORK = {
    ...
    DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    ),

提前非常感谢您。任何帮助将不胜感激

最佳答案

基本身份验证使用Authorization header 来传输用户和密码。 Django 休息also uses this header in the TokenAuthentication authentication backend 。 Nginx does not support multiple Authorization headers ,因此如果您尝试同时登录并使用 Token 身份验证,事情将会崩溃。

无需更改 Django 应用程序的解决方案是在 nginx 中使用另一种身份验证方式,例如 client certificates ,或者,您可以使用 ngx_http_auth_request_module检查签名 session cookie 是否已设置/有效,或者请求 IP 是否在(临时)白名单中,否则将用户重定向到带有登录表单的页面。

关于django - Nginx密码认证不断提示输入密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057631/

相关文章:

python - 仅针对经过身份验证的用户的 Django 缓存

Android - Retrofit 2 - 验证器结果

ssl - 在 Kubernetes 和 NGINX 中使用自签名证书设置 mTLS

nginx - 尝试为 Rancher 2.X 配置 Nginx - 从 Rancher 1.x 迁移

django基于外键的动态表单值

python - 覆盖 Django 的 RelatedManager 方法

python - 匹配Django中所有没有前缀的url

node.js - Passport -facebook Strategy.parseErrorResponse

PHP:验证本地 unix 用户(不直接访问/etc/shadow)

PHP 不适用于 https,而是下载文件