【问题标题】:.htaccess check for cookie first, then valid-user.htaccess 先检查 cookie,然后检查有效用户
【发布时间】:2012-10-24 11:14:18
【问题描述】:

我的 apache 服务器上有一个目录的 .htaccess 文件。目前这使用 mod_auth_mysql 进行用户验证以查看目录列表。但是,如果用户已经登录到我的应用程序(因此存在 cookie),我想跳过有效用户要求,从而消除多次登录。

当前 .htaccess

AuthName "Please don't hack the server, cheers"
AuthBasicAuthoritative Off
AuthUserFile /dev/null
AuthMySQL On
AuthType Basic
Auth_MYSQL on
Auth_MySQL_Host localhost
Auth_MySQL_User username
Auth_MySQL_Password password
AuthMySQL_DB auth
AuthMySQL_Password_Table users
Auth_MySQL_Username_Field username
Auth_MySQL_Password_Field password
Auth_MySQL_Empty_Passwords Off
Auth_MySQL_Encryption_Types Plaintext
Auth_MySQL_Authoritative On
require user james

Cookie 检查

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !^cookiename=ok$
RewriteRule .* / [NC,L]

如何正确结合这两者,首先检查 cookie,如果找到则允许通过,然后如果未找到 cookie,则检查有效用户?

谢谢

【问题讨论】:

    标签: linux apache .htaccess


    【解决方案1】:

    您需要在这里做两件事,您可以使用SetEnvIf directive 创建一个环境变量,然后对照Cookie 检查它。然后您需要告诉 mod_auth_mysql 允许 任何 组要求来满足身份验证。

    首先是环境变量:

    SetEnvIf Cookie cookiename=ok norequire_auth=yes
    

    cookiename=ok 是与 Cookie: HTTP 标头匹配的正则表达式。然后Satisfy

    Order Deny,Allow
    Satisfy Any
    
    # your auth stuff:
    AuthName "Please don't hack the server, cheers"
    AuthBasicAuthoritative Off
    AuthUserFile /dev/null
    AuthMySQL On
    AuthType Basic
    Auth_MYSQL on
    Auth_MySQL_Host localhost
    Auth_MySQL_User username
    Auth_MySQL_Password password
    AuthMySQL_DB auth
    AuthMySQL_Password_Table users
    Auth_MySQL_Username_Field username
    Auth_MySQL_Password_Field password
    Auth_MySQL_Empty_Passwords Off
    Auth_MySQL_Encryption_Types Plaintext
    Auth_MySQL_Authoritative On
    require user james
    
    Allow from env=norequire_auth
    

    所以Satisfy Any 表示如果满足require user james,我们允许如果满足Allow from env=norequire_auth。如果没有满足,则需要满足两条线才能允许访问。

    请注意,cookie 很容易被伪造,而且此系统不是保护您的网页的好方法。您至少需要检查 cookie 的值以获取会话 ID 或其他内容。

    【讨论】:

    • 感谢您的支持 - 但是,在将 SetEvnIf 添加到 .htaccess 顶部和接下来的“全部满足”之后,它似乎让任何人都无需任何身份验证即可通过。 httpd.conf 中的任何内容都可以覆盖我需要查看的内容吗?
    • @Jimbo 是否还有另一个“允许来自”的地方?
    • 不,有一个 deny from all 丢失 - 我编辑了,我希望没关系。谢谢你的帮助! :)
    • 嘿@Jon Lin,作为这类事情的专家,您可能有兴趣看到我的后续问题,即为什么它停止工作:stackoverflow.com/questions/13396655/… - 简单点? :)
    猜你喜欢
    • 2016-04-18
    • 2011-02-02
    • 2022-12-29
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多