我按照Laravel文档并成功安装了Passport . 一切都工作正常,但当我想通过 scope 中间件保护路线时,我总是得到 401 unauthorized .
scope
401 unauthorized
当我将中间件更改为 auth:api 时一切正常 .
auth:api
我检查了请求标头, Bearer 始终存在 .
Bearer
任何关于为什么 auth:api 中间件工作但 scope 中间件没有的想法?
实际上你需要同时使用它们来完成这项工作 . 您应该为整个API组留下 auth:api (这将验证令牌并找出它所属的用户),并为要使用特定范围保护的路由另外定义set scope (或 scopes )中间件 . 例如:
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 中间件 .
$routeMiddleware
1 回答
实际上你需要同时使用它们来完成这项工作 . 您应该为整个API组留下
auth:api
(这将验证令牌并找出它所属的用户),并为要使用特定范围保护的路由另外定义setscope
(或scopes
)中间件 . 例如:以上假设,您根据documentation在
$routeMiddleware
中注册了scope
/scopes
中间件 .