我的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的情况下检查有效用户?
谢谢
最佳答案
您需要在此处做两件事,您可以使用 SetEnvIf
directive创建一个环境变量,并根据Cookie
进行检查。然后,您需要告诉mod_auth_mysql允许任何要求的集合以满足身份验证。
首先是环境变量:
SetEnvIf Cookie cookiename=ok norequire_auth=yes
cookiename=ok
是与 Cookie: HTTP header 匹配的正则表达式。然后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的值以获取 session ID或其他内容。
关于linux - .htaccess首先检查cookie,然后再检查有效用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13218206/