Laravel-Passport / SPA 401未经授权

时间:2018-08-26 17:27:14

标签: php laravel vue.js single-page-application

我已经按照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中间件来确保您已通过身份验证,这是基于会话的身份验证:

enter image description here

更新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 eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImY5OGNiZjBhYmNjZGIyNzk1ZDRhOTllODEwZTQxMzFhMjMwNzhhNzVj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GQ2ZThlMDk4YWQ4N2ZiNzRiMjJlZTVkMjM1MjZkYjAyNTZkMTdkIn0%3D
Host: spa.test
Origin: http://spa.test
Referer: http://spa.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 来处理令牌的任何想法第三方申请?

3 个答案:

答案 0 :(得分:1)

为默认的oauth路由分配了网络和auth中间件,因此您必须找到一种为它们分配auth:api中间件的方法。

为了一致性,我也更喜欢使用api / oauth前缀。

在您的AuthServiceProvider中

git push gitlab master-gitlab

这将为您提供api / oauth前缀,并添加auth:api中间件,以便您可以通过Bearer令牌进行身份验证。

请记住,您会遇到一些使用CSRF令牌的问题

Passport::routes(null, array('prefix' => 'api/oauth', 'middleware'  =>  array('auth:api', 'web', 'auth')));

似乎没有解决。

您需要传递适当的标题(请参见https://laravel.com/docs/6.x/passport#consuming-your-api-with-javascript下的CSRF保护):

  

使用这种身份验证方法时,您需要确保请求中包含有效的CSRF令牌标头。默认的Laravel JavaScript支架包含一个Axios实例,该实例将自动使用加密的XSRF-TOKEN cookie值在同源请求中发送X-XSRF-TOKEN标头。

答案 1 :(得分:1)

大家好,我有同一期未经身份验证的问题。 但是我的错是我设置了 Authorization:'Bearer token'。,它替换为Postman Header。 Authorization:Bearer令牌。 现在可以了

答案 2 :(得分:0)

不久前,我们经历了同样的问题,但是我看到我们遵循laravel documentationkernel.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']);