我已经注册了Laravel Passport文档中提到的Vue组件,它们也显示在我的应用程序中:
<passport-clients></passport-clients>
<passport-authorized-clients></passport-authorized-clients>
<passport-personal-access-tokens></passport-personal-access-tokens>
但是,它们没有显示任何令牌/客户端,而且我也无法创建任何令牌/客户端,因为我的应用程序说我“未经授权”:
GET http://spa.test/oauth/personal-access-tokens 401
GET http://spa.test/oauth/clients 401 (Unauthorized)
GET http://spa.test/oauth/tokens 401 (Unauthorized)
GET http://spa.test/oauth/scopes 401 (Unauthorized)
这是我的AuthServiceProvider.php
public function boot() {
$this->registerPolicies();
Passport::routes();
}
我还把这个特性包括在我的app/User.php
中:
use HasApiTokens, Notifiable;
记住,我使用Laravel作为后端,Vue.js作为前端,这给了我一个SPA。
这是我的routes/api.php
:
Route::middleware('auth:api')->group(function () {
Route::get('/user', function (Request $request) {
return $request->user();
});
});
这是我的routes.js
:
const routes = [
{
path: '/api/*',
},
/**
* Handle Authentication
*/
{
path: '/',
alias: '/login',
name: 'login',
component: Login,
beforeEnter: ifNotAuthenticated,
}
/**
* Handle logged in pages
*/
{
path: '/dashboard',
name: 'layout',
component: MasterLayout,
beforeEnter: ifAuthenticated,
children: [{
path: '/dashboard',
name: 'dashboard',
component: Dashboard
}, {
path: '/home',
name: 'home',
component: Home
}]
},
];
更新1:
我发现问题是我使用Passport进行身份验证(auth:api
)所有的护照路由都使用Web中间件来确保您经过身份验证,这是一个基于会话的身份验证:
更新2:
请参阅我的请求标题:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: da-DK,da;q=0.9,en-US;q=0.8,en;q=0.7,nb;q=0.6
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImY5OGNiZjBhYmNjZGIyNzk1ZDRhOTllODEwZTQxMzFhMjMwNzhhNzVjYWMzYmM0Y2I2Yzg3NjM3ZDFlMzdkNmI3YzU4YWIzNWM4YmU5MDIxIn0.eyJhdWQiOiIyIiwianRpIjoiZjk4Y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a.test/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
X-CSRF-TOKEN: v9z5uFoGWPGprc3kJaCMhLpHGbvgQ7usXLwZu11e
X-Requested-With: XMLHttpRequest
X-XSRF-TOKEN: eyJpdiI6IkhrQnB1SCs5dWV4OU8zck10djZRbEE9PSIsInZhbHVlIjoieFwvZTJCUFVRZ0haQWNZVjBvUkJ2Q1FTSGtGTHM5SjBXTndOeXBFc2o2Q3pjclRRbjFqeHVpclJOQ3FPTUdKQ2t4M0g4bE5VUXRcL0twdXFNR25IT1NBdz09IiwibWFjIjoiYjI5MzRkNTEwMWZhNWUzNTI5ZTY2OTg5YTYxODQ3MDBkMTU2ZjJjZTE3YzdhZDc2ODMwNjI0NzlmMmQyZTY1NyJ9
关于如何仍然使用Vue作为前端的任何想法,Laravel作为后端并仍然使用Laravel Passport来处理第三方应用程序的令牌?
不久前我们经历了同样的问题,但我可以看到我们跟着laravel documentation在kernel.php
中添加了一个web中间件:
'web' => [
// Other middleware...
// ...
// This Adds a cookie containing a JWT token for Laravel Passport
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
可能相关:为了简化我们对后端的请求,我们让axios设置为api/
的请求前缀,这意味着我们还必须告诉Passport期待这一点。在AuthServiceProvider.php
:
Passport::routes(null, ['prefix' => 'api/oauth']);