Linux .htaccess首先检查cookie,然后检查有效用户

Linux .htaccess首先检查cookie,然后检查有效用户,linux,apache,.htaccess,Linux,Apache,.htaccess,我的apache服务器上有一个目录的.htaccess文件。目前,它使用mod_auth_mysql进行用户验证,以查看目录列表。但是,如果用户已经登录到我的应用程序(因此存在cookie),我希望跳过有效的用户需求,从而消除多次登录 当前.htaccess AuthName "Please don't hack the server, cheers" AuthBasicAuthoritative Off AuthUserFile /dev/null AuthMySQL On AuthType

我的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时允许通过,然后在找不到cookie时检查有效用户


谢谢

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

首先是环境变量:

SetEnvIf Cookie cookiename=ok norequire_auth=yes
cookiename=ok
是一个与Cookie:HTTP头匹配的正则表达式。然后,
满足

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
因此
满足Any
意味着如果
要求用户james
满足
允许来自env=norequire_auth
满足,则我们允许。如果没有“满足”,则需要满足这两条线路才能允许访问


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

谢谢您这样做-但是,在将SetEvnIf添加到.htaccess的顶部和下一个“满足所有人”之后,似乎任何人都可以通过,而无需任何身份验证。有没有可能httpd.conf中的某些内容会覆盖我需要查看的内容?@Jimbo是否有另一个“允许来自”的地方?没有,有一个来自所有
缺失的
拒绝-我编辑过,希望没问题。谢谢你的帮助!:)嘿@Jon Lin,作为这方面的专家,你可能有兴趣看看我的后续问题,关于为什么这会停止工作:-简单点?:)