首页 文章

Laravel护照范围中间件返回401

提问于
浏览
0

我按照Laravel文档并成功安装了Passport . 一切都工作正常,但当我想通过 scope 中间件保护路线时,我总是得到 401 unauthorized .

当我将中间件更改为 auth:api 时一切正常 .

我检查了请求标头, Bearer 始终存在 .

任何关于为什么 auth:api 中间件工作但 scope 中间件没有的想法?

1 回答

  • 0

    实际上你需要同时使用它们来完成这项工作 . 您应该为整个API组留下 auth:api (这将验证令牌并找出它所属的用户),并为要使用特定范围保护的路由另外定义set scope (或 scopes )中间件 . 例如:

    Route::group(['prefix' => 'api', 'middleware' => ['auth:api']], function () {
                Route::get('/route-for-any-scope', 'Api\YourController1@index');
                Route::get('/route-for-scope1-only', 'Api\YourController2@index')->middleware('scope:scope1');
    }
    

    以上假设,您根据documentation$routeMiddleware 中注册了 scope / scopes 中间件 .

相关问题