Laravel Passport 401以下文档

时间:2018-05-17 03:47:24

标签: php laravel oauth-2.0

我按照文件使用larval 5.6安装幼虫护照。目前,当我使用api后卫击中任何api路线时,我将在请求中获得401 http状态。我看起来扔了数百或似乎讨论这个主题,似乎找不到有用的东西。任何帮助都会很棒。

  1. 与作曲家一起安装
  2. php artisan passport:安装
  3. 将HasApiTokens放入用户模型
  4. 护照::路线();在Auth提供商
  5. 在config / auth.php中更改了提供程序
  6. 把它放在web中间件\ Laravel \ Passport \ Http \ Middleware \ CreateFreshApiToken :: class,
  7. 我的请求标题

    Cache-Control: no-cache, private
    Connection: keep-alive
    Content-Type: application/json
    Date: Thu, 17 May 2018 03:34:11 GMT
    phpdebugbar-id: Xe76c4a977b813e9296e0705b5cd6bc05
    Server: nginx
    Transfer-Encoding: chunked
    X-Powered-By: PHP/7.2.4
    X-RateLimit-Limit: 60
    X-RateLimit-Remaining: 59
    POST /api/chat HTTP/1.1
    Host: ct.test
    Connection: keep-alive
    Content-Length: 33
    Pragma: no-cache
    Cache-Control: no-cache
    Accept: application/json, text/plain, */*
    Origin: http://ct.test
    X-XSRF-TOKEN: eyJpdiI6IkQ3K3o3SExMYVZEWnYyejJQXC9zUlpnPT0iLCJ2YWx1ZSI6Ijh6UWVtdDlzU0hIWnpqZmdmazhSc0FMWUU5MTQ1N1dXNStZVlk3amhIbjNmYmlhN3BwaEJwbTI0MU5qUjVFRUtqWWV2aW1Td3JvdDg0QTd6RWJzem9RPT0iLCJtYWMiOiJkMDE0MmU2NGY5NmY3YmIxZjAwMjI4OGM1ZDUzMTU5OTBmMjNkM2YxOTZkNzM5NTAyMzliZjc0YTgyMTUyZWQ4In0=
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36
    Content-Type: application/json;charset=UTF-8
    Referer: http://ct.test/chat/new
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9
    Cookie: la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zc4NTVlYzY3MWUwZjkwNzZlOWVhNDY3OWQ5MmNlOTc0MGM1MDg1YmViNDNjYjQ0M2Q0MDkifQ%3D%3D
    

    我的路线看起来像这样

    Route::middleware('auth:api')->group(function () {
        Route::post('/chat', 'ChatController@store');
    });
    

1 个答案:

答案 0 :(得分:1)

您正在混合API策略。你引用了这个:

  

此Passport中间件会将laravel_token cookie附加到您的外发响应中。 ...

只有在您想要内部使用您的应用程序时才会这样。哪个没问题,但正如您在评论中指出的那样,您必须使用web中间件,而不是auth:api中间件。

这当然假设您正在使用axios发出请求并保留了Laravel附带的默认javascript,会自动将x-csrf-token注入axios标头。没有它,这将无效。