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

时间:2012-11-04 11:44:29

标签: 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 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的有效用户?

由于

1 个答案:

答案 0 :(得分: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,则表示我们允许{strong}满足。如果没有满足,则需要满足两条线以允许访问。

请注意,Cookie很容易伪造,而且此系统不是保护您网页的好方法。您至少需要检查cookie的会话ID值等。

相关问题