laravel-5 - 如何修复广播身份验证的 419 错误

标签 laravel-5 broadcast pusher

在我的 Laravel Framework 5.7/Vuejs 2.6 中,我使用 jwt.auth 并安装 pusher

我检查我的页面的 xsfr token https://imgur.com/a/CveJ6jh但我在 http://local/broadcasting/auth 中收到 419 错误请求并打开此 URL 的响应我看到页面过期消息。
在 app/Events/NewMessage.php 中:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Message;

class NewMessage
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        $this->message= $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('messages.'.$this->message->to);
    }
    public function broadcastWith()
    {
        return ['messages' => $this->message];
    }

}

在 routes/channels.php 中:

Broadcast::channel('messages.{id}', function ($user, $id) {
    dd($user->id, $id);
    return (int) $user->id === (int) $id;
});

但是我没有从上面看到任何调试信息。

我使用了之前应用中的推送器参数。我想他们一定没问题。否则我会有不同的错误?

在 vue 组件中:

mounted() {
    Echo.private(`messages.${this.customerRow.id}`)
        .listen('NewMessage', (e) => {
            this.hanleIncoming(e.message);
        });

我错过了什么?

谢谢!

最佳答案

这个错误的原因是 Laravel 期望 a CSRF token .

你有两个选择:

  1. Include the CSRF token (更好的安全性)
  2. Disable CSRF for this URL

关于laravel-5 - 如何修复广播身份验证的 419 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55824237/

相关文章:

javascript - 无法处理的实体错误(ajax)(laravel 5.2)

php - 如何从表中选择除外部表中的值之外的所有值?

php - Laravel Autoprefixer 不能在 webpack 中工作

node.js - 来自客户端的 socket.io 广播

java - 在单例中使用广播变量的优缺点是什么?

php - Laravel 5.4 事件广播不适用于 vuejs

javascript - 如何使用 Pusher pubnub socketio 将事件发送到单个客户端

php - 查询 SQL 无法与 Laravel 5 Controller 一起使用

android - UDP 广播 : Motorola block incoming ports?

python - PusherBadRequest 未知 Auth_Key