【问题标题】:How to check if the request header contains authorization in nginx如何检查请求标头是否包含nginx中的授权
【发布时间】:2017-04-18 14:22:15
【问题描述】:

我在post 上读到 Nginx 不支持多个授权标头。

我想知道如果我将如何签入 http 请求 存在授权标头。

基本上我正在向我的网页添加一个基本身份验证,因为它还没有准备好投入生产。我的站点是一个单页应用程序,并且我已成功在索引页面中添加了身份验证,但我的站点也具有登录功能。当我登录时,它不断要求再次进行身份验证。我是 nginx 新手,我不太确定如何解决这个问题

 location / {

        root /path/to/my/app/root/folder;
        index index.html index.php;

        #I want to only executed these lines only on the index page and login page 
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

【问题讨论】:

    标签: nginx basic-authentication


    【解决方案1】:

    您可以使用curl查看标题:

    $ curl -v -u your_user_name "http://......."
    

    查找包含Base64 编码的user:pass> Authorization: Basic ... 行。

    您可以使用以下方法解码字符串:

    printf auth_string | base64 --decode
    

    更多详情here.


    另外,请确保/etc/nginx/.htpasswd 拥有nginx 能够读取它的正确权限,并且它包含nginx (info here) 可识别格式的用户/通行证凭据:

    1.纯文本:

        # comment
        name1:password1
        name2:password2:comment
        name3:password3
    

    2。加密/散列:

    • 使用 crypt() 函数加密;可以使用 Apache HTTP Server 发行版中的“htpasswd”实用程序或
      “openssl passwd”命令;

    • 使用基于 MD5 的密码算法 (apr1) 的 Apache 变体进行散列;可以使用相同的工具生成;

    • RFC 2307 中描述的“{scheme}data”语法(1.0.3+)指定;目前实施的计划包括PLAIN(一个 示例一,不应该使用),SHA(1.3.13)(plain SHA-1 散列,不应使用)和 SSHA(盐渍 SHA-1 散列,使用 通过一些软件包,特别是 OpenLDAP 和 Dovecot)。

    $ htpasswd 
    Usage:
        htpasswd [-cimBdpsDv] [-C cost] passwordfile username
        htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password
    
        htpasswd -n[imBdps] [-C cost] username
        htpasswd -nb[mBdps] [-C cost] username password
     -c  Create a new file.
     -n  Don't update file; display results on stdout.
     -b  Use the password from the command line rather than prompting for it.
     -i  Read password from stdin without verification (for script usage).
     -m  Force MD5 encryption of the password (default).
     -B  Force bcrypt encryption of the password (very secure).
     -C  Set the computing time used for the bcrypt algorithm
         (higher is more secure but slower, default: 5, valid: 4 to 31).
     -d  Force CRYPT encryption of the password (8 chars max, insecure).
     -s  Force SHA encryption of the password (insecure).
     -p  Do not encrypt the password (plaintext, insecure).
     -D  Delete the specified user.
     -v  Verify password for the specified user.
    On other systems than Windows and NetWare the '-p' flag will probably not work.
    The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-02
      • 2020-05-06
      • 2012-08-12
      • 2021-08-28
      • 2020-09-09
      • 2019-01-01
      • 2015-12-06
      • 1970-01-01
      相关资源
      最近更新 更多