我在 Laravel 5.6 中。我构建了所有 API 路由并正确响应来 self 的 REST 客户端 (Paw) 的请求。我正在尝试构建一个简单的前端来访问这些路由。
我正在尝试尽可能多地使用 Laravel 的开箱即用功能,因此我使用 Axios 从使用 Vue.js 的 blade 模板调用这些路由。如果我在测试路由上禁用 auth 中间件,它会工作,但是当为路由启用 auth 中间件时,我在控制台上收到 401 错误。
问题似乎很明显...我的 /api
路由上的 auth:api
守卫希望在 header 中看到 oauth token ,但是当我登录时使用网页进行 session 身份验证。我假设有一种简单的方法可以解决这个问题,而不必在 Web 前端欺骗 oauth token 请求,对吧?我是否需要以某种方式在 Axios 请求中传递 session token ?而且,如果是这样,我是否还需要更改我的 api 路由文件中的 auth:api
守卫?
最佳答案
我解决了!我有点尴尬,因为答案实际上在 Laravel 文档中,但我会说我在将问题发布到这里之前试过了,但它没有用。也许当时有别的东西坏了。
根据 Laravel docs :
All you need to do is add the
CreateFreshApiToken
middleware to yourweb
middleware group in yourapp/Http/Kernel.php
file:'web' => [ // Other middleware... \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class, ],
This Passport middleware will attach a
laravel_token
cookie to your outgoing responses. This cookie contains an encrypted JWT that Passport will use to authenticate API requests from your JavaScript application. Now, you may make requests to your application's API without explicitly passing an access token...
关于laravel - 如何在 Laravel 中验证 API 路由的 Vue.js/Axios 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50193349/