Apache mod_auth_form如何锁定文件夹

Uwe*_*Uwe 5 apache authentication

在我看来,我有一个根本的误解,"mod_auth_form"应该如何工作.我参考Apache文档的这个页面:

http://httpd.apache.org/docs/current/mod/mod_auth_form.html
Run Code Online (Sandbox Code Playgroud)

我有一个公用文件夹和一个私人文件夹

我想要实现的是文件夹被锁定.用户需要使用他们的用户名和密码登录才能看到我的受保护文件夹的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>
Run Code Online (Sandbox Code Playgroud)

这是我的登录表单,它位于我的虚拟服务器的公共文件夹中:

<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>
Run Code Online (Sandbox Code Playgroud)

好的,这是发生的事情

  1. 去'customform.uwe'很好地工作 - >我看到我的索引页面显示该文件夹
  2. 转到'customform.uwe/login.html' - >我看到我的登录表单出现了,我可以登录并重定向到我的秘密'文件夹'的'索引'页面
  3. 转到'customform.uwe/secret/index.php'告诉我我的索引pagge是否登录.

我的问题在这里:

  1. 如何保护我的秘密文件夹,以便将未登录的用户重定向到登录表单.
  2. 这是正确的方法吗?

我此刻正在撞墙,所以非常感谢你的帮助.


好的,我相信我现在已经整理好了.我一定有点困惑:-)

我遵循的想法包括两件事:

  1. 提供登录功能
  2. 如果用户访问他/她需要进行身份验证的页面 - 而他/她不是 - 将用户重定向到登录页面

为此,我需要编辑两个文件:

  1. 我的虚拟主机
  2. 我的登录文件

这是虚拟主机:

<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>
Run Code Online (Sandbox Code Playgroud)

这是登录表单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>
Run Code Online (Sandbox Code Playgroud)

这似乎有用,它在Apache文档中都是(或多或少),但我感到困惑,因为它们没有完整的例子