在nginx上使用一次性cookie的非常简单的身份验证

Ash*_*Ash 14 authentication cookies nginx http-authentication password-protection

我有一个网站,仅供3名程序员私下使用.它是由nginx直接提供的简单HTML,但旨在用于办公室内外.

我想要一个简单的密码或身份验证方案.我可以使用HTTP身份验证,但这些往往会经常过期,这使人们很难使用.我也很紧张,有人嗅到比饼干更容易.

所以我想知道我是否可以在JavaScript中使用唯一的长ID在浏览器上设置cookie,并以某种方式告诉nginx只接受具有此cookie的请求(对于特定的子域).

这很简单吗?我如何能

  1. 告诉nginx按cookie过滤
  2. 在浏览器中,设置一个永不过期的cookie?

dav*_*djb 14

要通过cookie过滤Nginx,您可以在cookie 存在时执行某些操作,然后对有权访问的3个人执行实际操作,例如:

server {
    listen 443 ssl http2;  # Use HTTPS to protect the secret
    ...
    if ($http_cookie !~ 'secretvalue') {
        return 401; 
    }
    location / {
       # Auth'd behaviour goes here
    }
}
Run Code Online (Sandbox Code Playgroud)

要设置永不过期的cookie,请在服务器主机名上的页面上启动浏览器的JavaScript控制台,然后输入:

document.cookie = 'cookie=secretvalue;max-age=3153600000;path=/;secure';
Run Code Online (Sandbox Code Playgroud)

这在技术上并非永远,但100年应该这样做.expires=如果您愿意,也可以使用RFC1123格式的绝对日期,如果需要,可以轻松调整path.这也会secure在cookie上设置标志,因此它只会通过HTTPS发送.

还有一些浏览器插件可以让您创建任意cookie,但所有现代浏览器都有一个JavaScript控制台.

  • 如果 cookie 以明文形式发送,则会将秘密值泄露到公共互联网,这看起来不太好。我认为使用 TLS 足以保护秘密,对吧?您还面临跨站点攻击的风险,对吧? (2认同)

Jos*_*phH 9

我从Christian Stocker的博客文章中找到了一个非常简单的解决方案.它实现了以下规则:

  1. 如果用户使用内部IP,则允许他们使用.
  2. 如果用户设置了cookie,则允许它们.
  3. 如果两者都不匹配,则向用户显示http基本身份验证,如果他们成功进行身份验证,则设置长期cookie

这真是两全其美.

这是配置:

map $cookie_letmein $mysite_hascookie {
  "someRandomValue" "yes";
  default           "no";
}

geo $mysite_geo {
  192.168.0.0/24 "yes": #some network which should have access
  10.10.10.0/24  "yes": #some other network which should have access
  default        "no";
}


map $mysite_hascookie$mysite_geo $mysite_authentication{
  "yesyes" "off";  #both cookie and IP are correct  => OK
  "yesno"  "off"; #cookie is ok, but IP not  => OK
  "noyes"  "off";  #cookie is not ok, but IP is ok => OK
  default  "Your credentials please"; #everythingles => NOT OK
}

server {
  listen 80;
  server_name mysite.example.org;
  location / {
    auth_basic  $mysite_authentication;
    auth_basic_user_file  htpasswd/mysite;
    add_header Set-Cookie "letmein=someRandomValue;max-age=3153600000;path=/"; #set that special cookie, when everything is ok
    proxy_pass http://127.0.0.1:8000/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}
Run Code Online (Sandbox Code Playgroud)