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

标签 authentication cookies nginx password-protection http-authentication

我有一个仅供 3 位程序员私有(private)使用的网站。它是由 nginx 直接提供的简单 HTML,但旨在供办公室内外使用。

我想要一个简单的密码或身份验证方案。我可以使用 HTTP 身份验证,但这些往往会经常过期,这让人们使用起来很痛苦。我也很紧张,有人闻比 cookies 更容易闻。

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

这足够简单吗?我如何

  • 告诉 nginx 通过 cookie 过滤
  • 在浏览器中,设置一个永不过期的cookie?
  • 最佳答案

    我从 a blog post by Christian Stocker 找到了一个看起来非常简单的解决方案.它执行以下规则:

  • 如果用户在内部 IP 上,则允许他们使用。
  • 如果用户设置了 cookie,则允许他们使用。
  • 如果两者都不匹配,则向用户提供 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;
      }
    }
    

    关于authentication - 在 nginx 上使用一次性 cookie 进行非常简单的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718895/

    相关文章:

    authentication - 如何使用 Post 方法获取 OAuth2 访问 token

    java - 是否可以合并 java\jre\security\lib\cacerts 文件

    python - redisai 客户端密码/认证过程

    React-native 从 WebView 获取 cookies

    ajax - 如何在 wordpress ajax 请求处理程序中设置 cookie?

    node.js - nginx 和 socket.io 解决方法

    nginx - 有没有办法配置 Nginx 同时将传入请求广播到多个上游服务器?

    php - 如何在laravel网页中使用token认证

    android - 发布 JSOUP 文档以登录网站

    Tomcat 的 Nginx 反向代理