记一次神奇的 bug,无限 302 重定向

一切按照教程走,居然出现了无限302,user和admin两个地址互相重定向的问题。于是各种检查问题,甚至复制粘贴情况仍旧出现。
后来追到源码中发现。
Frozennode\Administrator\Http\Middleware

public function handle($request, Closure $next)
    {
        $config = app('itemconfig');

        //if the model doesn't exist at all, redirect to 404
        if (!$config) {
            abort(404, 'Page not found');
        }

        //check the permission
        $p = $config->getOption('permission');

        //if the user is simply not allowed permission to this model, redirect them to the dashboard
        if (!$p) {
            return redirect()->route('admin_dashboard');
        }
      /**这里的$p是子菜单users的权限**/
        //get the settings data if it's a settings page
        if ($config->getType() === 'settings') {
            $config->fetchData(app('admin_field_factory')->getEditFields());
        }

        //otherwise if this is a response, return that
        if (is_a($p, 'Illuminate\Http\JsonResponse') || is_a($p, 'Illuminate\Http\Response') || is_a($p, 'Illuminate\\Http\\RedirectResponse')) {
            return $p;
        }

        return $next($request);
    }

最终发现是,manage_users少了一个s,于是造成了用户有后台的进入权限,但是没有users菜单的权限。
但是进入后台的首页被重定向到了users页面,而users页面在没有权限的时候,根据return redirect()->route('admin_dashboard');可见被重定向到了菜单主页,也就是http://larabbs.test/admin,自此循环重定向构成。

所以能后台管理的用户一定也要有默认菜单的访问权限呀。(手动掩面)

Nickel
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1
Apress

我也遇到了,只有权限最高的角色能够登陆。
我觉得是权限判断的时候出了问题,然后我做了一下修改

因为可能是由于权限太高的问题导致,在 config/administrator 中全局搜索 ‘premiss’,所以把 config/administrator/user.php 中 的权限验证 修改:

// 设置当前页面的访问权限,通过返回布尔值来控制权限。
// 返回 True 即通过权限验证,False 则无权访问并从 Menu 中隐藏
'permission'=> function()
{
return Auth::user()->can('manage_users');
},
把修改 ‘manage_users’ 修改为 ‘manage_contents' 即可

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!