首页 文章

Laravel Passport“auth:api”中间件充当“web,auth”中间件

提问于
浏览
3

我已经为官方文档(https://laravel.com/docs/5.3/passport#introduction)中描述的Laravel 5.3设置了 Laravel Passport 包 .

我希望API由移动应用程序使用,所以我正在尝试实现 Password Grant Tokens . 我创建了一个密码授予客户端,以及令牌请求进程......

$response = $http->post('http://my-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'my@email.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);

...按预期工作,为我的一个用户返回 access-tokenrefresh-token .

一方面,

php artisan route:list

列出api / user URI的正确中间件: api,auth:api

api guard的驱动程序在config / auth.php中正确设置为 passport . 总而言之,安装过程的每一步都已完成(https://laravel.com/docs/5.3/passport#installation) .

默认api.php的内容:

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:api');

当我访问http://my-app.com/api/user时出现问题,因为它似乎是使用'web'中间件验证请求,而不是'api' ...当我访问时,如果用户未登录,我将被重定向到/ login(登录表单),和/如果它是...

任何帮助将非常感激 . 提前致谢 .

2 回答

  • 7

    解决了!仅供记录,解决方案:

    我发送请求到http://my-app.com/api/user与HTTP标头错误 . 我发送:

    Type: Authorization - Content: Bearer: $accessToken
    

    ......正确的方法是:

    Type: Authorization - Content: Bearer $accessToken (without colon)
    

    我从没想过这可能是一个错字......无论如何,错误并不容易被发现,因为重定向到登录表单从一开始就误导了我 . 我相信这确实是一种奇怪的行为......

  • 0

    正确的解决方案是从此文件中删除 redirectTo()app/http/middleware/Authenticate.php 中验证中间件

相关问题