在我看来,我对“mod_auth_form”应该如何工作有一个根本性的误解。我参考了 Apache 文档的这一页:
http://httpd.apache.org/docs/current/mod/mod_auth_form.html
我有一个公用文件夹和一个私人文件夹
我想要实现的是锁定一个文件夹。用户需要使用用户名和密码登录才能查看受保护文件夹的index.php 页面。
这是我的虚拟主机设置:
<VirtualHost *:80>
ServerName customform.uwe
DocumentRoot "/home/uwe/www/protected_custom_form"
DirectoryIndex index.php
ErrorLog /var/log/apache2/protected_custom_form.error.log
CustomLog /var/log/apache2/protected_custom_form.access.log combined
<Directory "/home/uwe/www/protected_custom_form">
AllowOverride All
Allow from All
</Directory>
<Directory "/home/uwe/www/protected_custom_form/secret/">
</Directory>
<Location /dologin>
SetHandler form-login-handler
AuthFormLoginRequiredLocation http://customform.uwe/login.html
AuthFormProvider file
AuthUserFile /home/uwe/www/conf/passwd
AuthType form
AuthName realm
Session On
SessionCookieName session path=/
SessionCryptoPassphrase secret
</Location>
</VirtualHost>
这是我的登录表单,它托管在我的虚拟服务器的公共文件夹中:
<form method="POST" action="/dologin">
Username: <input type="text" name="httpd_username" value="" />
Password: <input type="password" name="httpd_password" value="" />
<input type="submit" name="login" value="Login" />
<input type="hidden" name="httpd_location" value="http://customform.uwe/secret/index.php" />
</form>
好的,这就是发生的事情
- 转到“customform.uwe”效果很好 -> 我看到该文件夹的索引页正在显示
- 转到“customform.uwe/login.html”-> 我看到我的登录表单出现,我可以登录并重定向到我的秘密“文件夹”的“索引”页面
- 转到“custom form.uwe/secret/index.php”显示我的索引页面是否已登录。
这是我的问题:
- 如何保护我的秘密文件夹,以便将未登录的用户重定向到登录表单。
- 这是完全正确的方法吗?
我现在真的很头疼,所以非常感谢你的帮助。
好的,我相信我现在已经解决了。我一定有点困惑:-)
我遵循的想法包括两件事:
- 提供登录功能
- 如果用户转到需要验证的页面(而他/她不需要验证),则将用户重定向到登录页面
为了实现这一点,我需要编辑两个文件:
- 我的虚拟主机
- 我的登录文件
这是虚拟主机:
<VirtualHost *:80>
ServerName customform.uwe
DocumentRoot "/home/uwe/www/protected_custom_form"
DirectoryIndex index.php
ErrorLog /var/log/apache2/protected_custom_form.error.log
CustomLog /var/log/apache2/protected_custom_form.access.log combined
#This is the public
<Directory "/home/uwe/www/protected_custom_form">
AllowOverride All
Allow from All
</Directory>
#This is the login handler, the login form needs to pint to this handler in its action!
<Location /dologin>
SetHandler form-login-handler
AuthFormLoginRequiredLocation http://customform.uwe/login.html
AuthFormLoginSuccessLocation http://customform.uwe/secret/secretindex.php
AuthFormProvider file
AuthUserFile /home/uwe/www/conf/passwd
AuthType form
AuthName realm
Session On
SessionCookieName session path=/
SessionCryptoPassphrase secret
</Location>
# This is the location setting I missed earlier: When a
# user comes to this location unauthorised, he will be redirect to the login form
# This happens as the ErrorDoucment gets overwritten with login page
<Location /secret/index.php>
Require valid-user
AuthFormProvider file
ErrorDocument 401 /login.html
AuthUserFile /home/uwe/www/conf/passwd
AuthType form
AuthName realm
AuthFormLoginRequiredLocation http://customform.uwe/login.html
Session On
SessionCookieName session path=/
SessionCryptoPassphrase secret
</Location>
</VirtualHost>
这是登录表单 html
这里的变化是表单的操作处理程序现在指向我的位置
我上面定义的
<form method="POST" action="/dologin">
Username: <input type="text" name="httpd_username" value="" />
Password: <input type="password" name="httpd_password" value="" />
<input type="submit" name="login" value="Login" />
<input type="hidden" name="httpd_location" value="/secret/secretindex.php" />
</form>
这似乎有效,它(或多或少)都在 Apache 文档中,但我很困惑,因为它们不是完整的示例