路线上有多名警卫

ami*_*iio 3 php laravel laravel-routing laravel-5.2

我使用laravel框架,希望在自己的路线中使用多个警卫,例如:

   Route::group([ 'middleware' => 'jwt.auth', 'guard' => ['biker','customer','operator']], function () {}
Run Code Online (Sandbox Code Playgroud)

AuthServiceProvider.php在启动部分的下面有一个脚本:

  $this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $event) {
        $route = $event->route;
        if (!array_has($route->getAction(), 'guard')) {
            return;
        }
        $routeGuard = array_get($route->getAction(), 'guard');
        $this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
            return $this->app['auth']->guard($routeGuard)->user();
        });
        $this->app['auth']->setDefaultDriver($routeGuard);
    });
Run Code Online (Sandbox Code Playgroud)

这样只能在路由中使用一个守卫,'guard'=>'biker'
因此如何更改AuthServiceProvider.php中的代码以在路由中使用多个gaurd

Mar*_*tre 7

我知道这是一个老问题,但我只是研究了这个问题,并想出了自己解决问题的方法。这对其他人可能有用。解决方案非常简单,您只需在中间件名称之后指定每个防护,并用逗号分隔,如下所示:

Route::group(['middleware' => ['auth:biker,customer,operator'], function() {
    // ...
});
Run Code Online (Sandbox Code Playgroud)

然后将防护措施发送到\Illuminate\Auth\Middleware\Authenticate功能authenticate(array $guards),该功能检查阵列中提供的每个防护措施。

这适用于Laravel 5.4。