【问题标题】:Laravel passport scope middleware return 401Laravel 护照范围中间件返回 401
【发布时间】:2017-12-08 00:40:01
【问题描述】:
我遵循 Laravel 文档并成功安装了 Passport。一切正常,但是当我想通过scope 中间件保护路由时,我总是得到401 unauthorized。
当我将中间件更改为 auth:api 时,一切正常。
我检查了请求标头,Bearer 始终存在。
知道为什么auth:api 中间件有效而scope 中间件无效吗?
【问题讨论】:
标签:
laravel
scope
laravel-passport
【解决方案1】:
实际上,您需要同时使用两者才能完成这项工作。您应该为整个 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 中间件。