【问题标题】:How to fix 419 error with broadcasting auth如何使用广播身份验证修复 419 错误
【发布时间】:2019-09-13 09:46:57
【问题描述】:

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

我检查了我的页面 https://imgur.com/a/CveJ6jh 的 xsfr 令牌,但我在 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-5 broadcast pusher


    【解决方案1】:

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

    你有两个选择:

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

    【讨论】:

    • 我尝试了第二种方法并得到了错误:403 Forbidden。我不确定这是否正确?
    • 419 绝对是一个 CSRF 错误。 403 必须来自其他地方。你有授权中间件抛出它吗?
    • 正如我所写,我使用 jwt.auth 。我需要检查哪些授权中间件?
    • 查看 403 的堆栈跟踪以了解它被抛出的位置。
    【解决方案2】:

    要解决这个问题,您需要在广播路由上添加中间件,就像这样。

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

    这在 app/Providers/BroadcastServiceProvider.php 上

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2023-03-17
      • 1970-01-01
      • 2021-08-05
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2021-06-30
      相关资源
      最近更新 更多