【问题标题】:Nginx password authentication keeps prompting for passwordNginx密码认证一直提示输入密码
【发布时间】:2016-07-03 15:05:26
【问题描述】:

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

我正在使用 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',
    ),

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

【问题讨论】:

  • 您的 Django 站点是否也使用 HTTP 身份验证,但用户和密码不同? (我问是因为你写了“API 调用”;Django REST 框架确实支持basic authentication)。
  • 我不这么认为,但我不确定。我接受TokenAuthenticationOAuth2Authentication(我编辑了添加身份验证相关设置的问题,以防有帮助)。那正确吗?或者我是否可以限制我的 django 配置中的所有请求(不仅是那些需要身份验证的请求(我使用 Django Rest Framework 处理),而是所有请求,考虑到我根本不希望该站点被公开?跨度>
  • 那么你的冲突就出现了:基本的HTTP身份验证和TokenAuthentication都使用Authorization标头,如果他们看到对方的标头,两者都会抱怨(从他们的角度来看, 无效的)。 SessionAuthentication 是一个选项吗?
  • 我会说不,因为我已经在生产中使用它。但是,由于访问此开发服务器将受到限制,我想我可以更改设置。好的。我会试一试,然后回复你!

标签: django authentication nginx uwsgi


【解决方案1】:

基本身份验证使用Authorization 标头来传输用户和密码。 Django REST 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 检查签名会话 cookie 是否设置/有效或如果请求 IP 在(临时)白名单中,则将用户重定向到带有登录表单的页面。

【讨论】:

【解决方案2】:

使用 Nginx 进行基本身份验证有一种出色、简单且更安全的方法。就是,使用oAuth2_proxy,它具有基本身份验证支持,包括 Web 表单(非常好,浏览器不会保持连接活动)来输入用户/密码凭据,并可以选择包含像 htpasswd 这样的密码文件。它还支持许多其他身份提供者。 这是我的基本身份验证的 oAuth2_proxy 配置文件:

http_address = "0.0.0.0:5160" #use any port you want

upstreams = [
    "https://url-that-you-want-to-protect/"
]

request_logging = false
custom_sign_in_logo = "/etc/apache2/small-email-icon.png" #custom logo on form
authenticated_emails_file = "/etc/apache2/emails" #required option, file can be empty

htpasswd_file = "/etc/apache2/.htpasswd" #basic auth password file
display_htpasswd_form = true

client_id = "hfytr76r7686887"
client_secret = "ghgh6767ghgh7654fghj6543dfgh5432"
cookie_name = "_oauth2_proxy"
cookie_secret = "ghgh6gguujgh7654fghj6543dfgh5432"
cookie_expire = "2h15m0s"
# cookie_refresh = ""
cookie_secure = true
footer = "Unauthorized access prohibited."

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 2017-09-02
    • 2012-07-25
    • 2018-12-02
    • 2014-07-07
    • 2017-11-18
    • 1970-01-01
    相关资源
    最近更新 更多