【问题标题】:Check permissions on every page request, then redirect检查每个页面请求的权限,然后重定向
【发布时间】:2017-10-12 05:24:09
【问题描述】:

我在 apache (2.4) Web 服务器上编写了一个 Web 应用程序,用户通过 kerberos 进行身份验证。我的数据库中有一个用户表,其基本权限(r,w)设置为 0 或 1。现在我想在每次用户尝试访问此 Web 服务器上的页面时检查这些权限。当用户没有读取权限时,他应该被重定向到页面“forbidden.html”。显然,apache 无法进行权限检查。于是我用php试了一下。

我目前所拥有的

我已将所有 html 文件放在 apache DocumentRoot 的子文件夹 /html 中,并将其放入我的配置文件中,以便每次用户在 /html 中请求页面时调用 php/auth.php:

<Location /html>
    <If "%{REQUEST_URI} !~ m#\/forbidden\.html#">
            # Script checks if user should get access to the site
            SetHandler auth-script
            Action auth-script "/php/auth.php"
    </If>
</Location>

php/auth.php:

if ($params["user"]["r"] == 1) {
    // User has permission, redirect to the requested page
    header("Location: https://webapp.domain.de".$_SERVER["REQUEST_URI"]);
} else {
    // User has no permission, redirect to forbidden.html
    header("Location: https://webapp.domain.de/html/forbidden.html");
}

问题

我的问题是我得到了一个重定向循环。似乎来自 auth.php 的 header() 的重定向再次通过 apache 的 Action 指令触发了 auth.php 脚本。当用户被重定向到forbidden.html时不会出现此行为,因为它被排除在conf文件中。

我尝试在 auth.php 中注释掉 header(),但是 apache 不会使用 Action 指令自动为请求的站点提供服务。

有没有办法知道 auth.php 是否已经在这个请求中重定向了用户并告诉 apache 不要调用脚本。还是有更好的方法来实现我想要实现的目标?

提前致谢。

【问题讨论】:

    标签: php apache authentication redirect


    【解决方案1】:

    我在完全不使用 Apache 指令的情况下做了类似的事情(托管站点,无法更改任何内容)。您不需要 Apache 重定向部分。

    在每个 phtml 文件中,我做的第一个动作是需要一个类似于您的 auth.php 的 PHP 页面。如果 auth.php 检测到用户没有该页面的权限,我会发回一个标头来重定向他,如下所示:

    header('Location: http://www.example.com/forbidden.html');
    

    只需确保在调用标头之前没有打印任何其他内容,未经授权的用户将最终进入您的位置页面。

    如果用户被授权,没有标题,没有重定向,继续“向下”页面。

    【讨论】:

    • 感谢您的解决方案。我在之前的应用程序中这样做了。只是想要一种以某种方式集中的方法,而不是在每个文件中都添加一个要求。
    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多