首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JWT身份验证令牌在非/api路由中无效

JWT身份验证令牌在非/api路由中无效
EN

Stack Overflow用户
提问于 2019-02-27 02:59:42
回答 1查看 309关注 0票数 0

我对Laravel广播功能上的JWT身份验证令牌有问题。上面写着:{error: "token_invalid"}

我不知道为什么会发生这种情况,但似乎令牌没有问题。它适用于/api路由,但不适用于/broadcasting/auth等非api路由。它在jwt.io上也有效。

有什么解决方案吗?

代码语言:javascript
复制
#----------------------------------
# api.php
#----------------------------------

Route::group(['middleware' => 'jwt.auth'], function () {
    Route::apiResource('flights', 'Api\\FlightControllerAPI');
    Route::apiResource('airlines', 'Api\\AirlineControllerAPI');
});
代码语言:javascript
复制
#--------------------------------
# BroadcastServiceProvider.php
#--------------------------------

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Broadcast::routes();
        Broadcast::routes(['middleware' => ['jwt.auth']]);

        require base_path('routes/channels.php');
    }
}
代码语言:javascript
复制
#----------------------------
# app.js
#----------------------------

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap')

window.Vue = require('vue')

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

const files = require.context('./', true, /\.vue$/i)
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */
import Echo from "laravel-echo";

window.Pusher = require('pusher-js');

var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6Ly9mbGlnaHQtc2NoZWR1bGUudGVzdC9hcGkvYXV0aC9yZWZyZXNoIiwiaWF0IjoxNTUxMjA1MzAzLCJleHAiOjE1NTEyMDkzMzUsIm5iZiI6MTU1MTIwNTczNSwianRpIjoiV0xWVzU5c28xb0NmTmw5TiJ9.PR6j0bIamYdA6-yAxwVqkeaadp4uZ0XHvEOhJjVInMk"
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '38505c9887e0f1a6b7b2',
    cluster: 'ap1',
    encrypted: true,
    auth: {
        headers: {
            Authorization: "Bearer " + token
        }
    }
});

export const serverBus = new Vue()

/**
 * Since this was a single page application, we will no longer use blade
 * templating engine. The laravel itself will act as pure API provider.
 * 
 * To customize routes, you can do it here.
 */
import axios from 'axios';
import VueAxios from 'vue-axios';
import App from './views/App.vue'
import router from './routes'

axios.defaults.baseURL = '/api'

Vue.use(VueAxios, axios)

Vue.router = router

Vue.use(require('@websanova/vue-auth'), {
    auth: require('@websanova/vue-auth/drivers/auth/bearer.js'),
    http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
    router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
})

const app = new Vue({
    el: '#app',
    router,
    render: app => app(App),
})
EN

回答 1

Stack Overflow用户

发布于 2019-02-27 03:39:52

刚刚找到了解决这个问题的办法,在web.php中添加测试路由后,我尝试访问/test?token=current_token,但得到了一个this token has been blacklisted异常。

我只需在.env文件中添加一行:JWT_BLACKLIST_ENABLED=false来禁用它,它就会开始正常工作。

代码语言:javascript
复制
#--------------------
# web.php
#--------------------

Route::get('/test', function () {
    return JWTAuth::parseToken()->authenticate();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54892356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档