Laravel/broadcasting/auth始终失败并显示403错误 [英] Laravel /broadcasting/auth Always Fails With 403 Error

查看:671
本文介绍了Laravel/broadcasting/auth始终失败并显示403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近研究了Laravel 5.3的Laravel-Echo和Pusher组合.我已经成功建立了公共频道,并转到了私人频道.无论我如何尝试授权操作(直到并包括使用简单的return true语句),我都无法通过Laravel从/broadcasting/auth路由返回403.谁能告诉我我在做什么错?

I have recently delved into Laravel 5.3's Laravel-Echo and Pusher combination. I have successfully set up public channels and moved on to private ones. I am having trouble with Laravel returning a 403 from the /broadcasting/auth route, no matter what I do to try to authorize the action (up to and including using a simple return true statement). Can anyone tell me what I am doing wrong?

App/Providers/BroadcastServiceProvider.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();

        /*
         * Authenticate the user's personal channel...
         */
        Broadcast::channel('App.User.*', function ($user, $userId) {
            return true;
        });
    }
}

资源/资产/js/booststrap.js:

import Echo from "laravel-echo"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'My-Key-Here'
});

window.Echo.private('App.User.1')
    .notification((notification) => {
        console.log(notification.type);
    });

我可以在Pusher调试控制台中看到该事件及其有效负载,一旦它到达身份验证路由,它就失败了.

I can see the event and it's payload in my Pusher debug console, it is simply failing once it hits the auth route.

推荐答案

使用Laravel版本> 5.3&的错误403/broadcasting/auth Pusher,您需要使用

Error 403 /broadcasting/auth with Laravel version > 5.3 & Pusher, you need change your code in resources/assets/js/bootstrap.js with

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your key',
    cluster: 'your cluster',
    encrypted: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + YourTokenLogin
        },
    },
});

在app/Providers/BroadcastServiceProvider.php中更改

And in app/Providers/BroadcastServiceProvider.php change by

Broadcast::routes()

使用

Broadcast::routes(['middleware' => ['auth:api']]);

Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT

它为我工作,希望对您有帮助.

it worked for me, and hope it help you.

这篇关于Laravel/broadcasting/auth始终失败并显示403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆