【问题标题】:running laravel horizon in production throw a 403 error when requesting domain/horizon请求域/地平线时,在生产中运行 laravel Horizo​​n 会抛出 403 错误
【发布时间】:2020-07-31 11:16:02
【问题描述】:

我的本​​地环境中有 testet laravel Horizo​​n,一切都按预期工作。当我切换到生产域/水平时会抛出 403 错误。我已经按照文档中的说明在 Horizo​​nServiceProvider 中设置了大门 - 第一步只是在没有身份验证的情况下获得访问权限。我的大门现在看起来像这样:

{
    Gate::define('viewHorizon', function ($user = null) {
        return true;
    });
}

谁能建议我缺少什么?

link to 403 error link to 401 error - Dashboard without data

【问题讨论】:

    标签: php laravel authentication horizon


    【解决方案1】:

    需要在App\Providers\HorizonServiceProvider类中添加如下方法:

        protected function authorization()
        {
            Horizon::auth(function () {
                return true;
            });
        }
    

    此方法会覆盖验证 HTTP 请求的父方法。

    【讨论】:

      【解决方案2】:

      对我来说,问题是我设置了app\Providers\HorizonServiceProvider.php

      /**
       * Register the Horizon gate.
       *
       * This gate determines who can access Horizon in non-local environments.
       *
       * @return void
       */
      protected function gate() {
          Gate::define('viewHorizon', function ($user) {//https://laravel.com/docs/7.x/horizon#dashboard-authorization            
              return $user->id === \App\Constants\Permissions::ADMIN_USER_ID;
          });
      }
      

      然后我忘记了我需要先登录 example.com/login 才能访问 example.com/horizo​​n。

      https://github.com/laravel/horizon/issues/563#issuecomment-500821983https://laravel.com/docs/8.x/horizon#dashboard-authorization

      【讨论】:

        【解决方案3】:

        查看此 GitHub 评论:https://github.com/laravel/horizon/issues/563#issuecomment-480882947

        您可能需要注册 Horizo​​n 的服务提供商。

        config/app.php:

        'providers' => [
                /*
                 * Application Service Providers...
                 */
                App\Providers\AppServiceProvider::class,
            ...
                App\Providers\TelescopeServiceProvider::class,
                App\Providers\HorizonServiceProvider::class,
            ],
        

        【讨论】:

        • 感谢您的回复 - 我已经这样做了,或者作曲家为我这样做了 :)
        • 您是否使用了非默认保护? GitHub 上的最后一条评论提到了这个具体案例。
        【解决方案4】:

        错误是因为horizo​​n首先进入了boot方法,所以我建议你在你的Horizo​​nServiceProvider.php中编辑boot方法来允许你这样的请求:

            /**
             * Bootstrap any application services.
             *
             * @return void
             */
            public function boot()
            {
                parent::boot();
        
                Horizon::auth(function ($request) {
                    if ($request->ajax()){
                        return true;
                    }
                    else if (isset($request->let_me_go) && $request->let_me_go == 'ok'){
                        return true;
                    }else{
                        throw new UnauthorizedHttpException('Unauthorized');
                    }
                });
            }

        所以当你去你的生产服务器时需要像这样传递参数:

        my-production-site.com/horizo​​n/dashboard?let_me_go=ok

        【讨论】:

        • 它让我更进一步!它现在加载仪表板,但无法获取数据。统计数据、主数据和工作负载调用返回 401 - 请参阅原始帖子中的链接
        • 为了测试 - 并继续前进,我只是将 else 语句更改为也返回 true。猜猜我现在会在没有 Auth 的情况下运行 :)
        猜你喜欢
        • 2017-05-30
        • 2019-07-01
        • 1970-01-01
        • 2022-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        相关资源
        最近更新 更多