Laravel望远镜- 403禁止访问

4ngedf3f  于 2023-04-22  发布在  其他
关注(0)|答案(4)|浏览(200)

我已经实现了Laravel望远镜,我只能访问如果APP_ENV=local
我遵循了Laravel的文档,并在TelescopeServiceProvider.php中更改了代码(注意我的环境被称为loca,dev,testing和prod)。
访问Telescope的唯一方法是在每个环境中更改APP_ENV=local
有谁知道哪些可以通过发行?
问候

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\TelescopeApplicationServiceProvider;

class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // Telescope::night();

        $this->hideSensitiveRequestDetails();

        Telescope::filter(function (IncomingEntry $entry) {
            if ($this->app->environment('local') || $this->app->environment('dev') || $this->app->environment('test') || $this->app->environment('prod')) {
                return true;
            }

            return $entry->isReportableException() ||
                   $entry->isFailedRequest() ||
                   $entry->isFailedJob() ||
                   $entry->isScheduledTask() ||
                   $entry->hasMonitoredTag();
        });
    }

    /**
     * Prevent sensitive request details from being logged by Telescope.
     *
     * @return void
     */
    protected function hideSensitiveRequestDetails()
    {
        if ($this->app->environment('local') || $this->app->environment('dev') || $this->app->environment('test') || $this->app->environment('prod')) {
            return;
        }

        Telescope::hideRequestParameters(['_token']);

        Telescope::hideRequestHeaders([
            'cookie',
            'x-csrf-token',
            'x-xsrf-token',
        ]);
    }

    /**
     * Register the Telescope gate.
     *
     * This gate determines who can access Telescope in non-local environments.
     *
     * @return void
     */
    protected function gate()
    {
        Gate::define('viewTelescope', function ($user) {
            return in_array($user->email, [
                //
            ]);
        });
    }
}

这是我的confif/app.php文件

/*
 * Application Service Providers...
 */
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
//App\Providers\TelescopeServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Laravel\Socialite\SocialiteServiceProvider::class,
mdfafbf1

mdfafbf11#

默认情况下,您只能在local环境下访问此 Jmeter 板

在您的app/Providers/TelescopeServiceProvider.php文件中,有一个gate方法。此授权门控制在非本地环境下对Telescope的访问。您可以根据需要自由修改此门,以限制对您的Telescope安装的访问:

/**
 * Register the Telescope gate.
 *
 * This gate determines who can access Telescope in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewTelescope', function ($user) {
        return in_array($user->email, [
            // Your users
            'user@yourapp.tld',
        ]);
    });
}
disbfnqx

disbfnqx2#

您可以在App\Providers\TelescopeServiceProvider中覆盖Laravel\Telescope\TelescopeApplicationServiceProvider中的authorization()方法,并包含您的环境名称。

但要小心

/**
 * Configure the Telescope authorization services.
 *
 * @return void
 */
protected function authorization()
{
    $this->gate();

    Telescope::auth(function ($request) {
        return app()->environment('local') ||
               Gate::check('viewTelescope', [$request->user()]);
    });
}

...
return app()->environment(['local', testing]) ||
       Gate::check('viewTelescope', [$request->user()]);
...
ff29svar

ff29svar3#

如果你使用laravel作为API应用,你可以使用auth by cookie。只需添加cookie secretTelescope并运行它。

Gate::define('viewTelescope', function (?User $user) {
    return array_key_exists('secretTelescope', $_COOKIE);
);
bxjv4tth

bxjv4tth4#

在我的例子中,我通过改变这个方法和.env值来解决这个问题
这位于此路径App\Providers\TelescopeServiceProvider.php

/**
     * Register the Telescope gate.
     *
     * This gate determines who can access Telescope in non-local environments.
     */
    protected function gate(): void
    {
        Gate::define('viewTelescope', function ($user) {
            return $user->username == 'super_admin';
        });
    }

这将工作,如果你看到它不工作,那么你应该检查你的.env文件
如果不存在,则添加此行

TELESCOPE_ENABLED=true

然后检查APP_ENV,如果是本地,则OK,如果不是,则更改为本地
我希望你的问题会得到解决

相关问题