【问题标题】:Getting AccessDeniedHttpException from RedisBroadcaster.php with laravel-echo-server使用 laravel-echo-server 从 RedisBroadcaster.php 获取 AccessDeniedHttpException
【发布时间】:2021-03-17 02:40:54
【问题描述】:

我不明白为什么我在尝试连接到私人频道时会遇到这个问题。 我有与这篇文章 100% 相同的代码 - Unable to authenticate laravel private channel using Laravel echo server, redis and socket.io 虽然使用 redis 前缀不起作用... 它适用于简单的频道... 这是我的错误-

客户端无法通过身份验证,通过 laravel-echo-server 获得 HTTP 状态 403

这是我的代码 js-

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
    auth: {
    headers:
        {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    }
});
window.Echo.private('user.'+userIdentifier).listen('SomeTestEvent', function (e) {
...
} 

routes/channels.php -

Broadcast::channel('user.{id}', function ($user, $id) {
    return true; // returning always true just for test
});

BroadcastServiceProvider.php -

public function boot()
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }

事件/SomeTestEvent.php -

public function broadcastOn()
    {
        return new PrivateChannel('user.'.$this->data['user_id']);
    }

laravel-echo-server.json -

{
    "authHost": "http://localhost:8000",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "e0ebecd04673b905",
            "key": "39c2a1314e4ef8e2879486a4a3b91c1e"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "host": "localhost",
            "port": "6379"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": "",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

【问题讨论】:

    标签: laravel redis laravel-echo


    【解决方案1】:

    天哪,我才知道怎么做。

    我需要使用 - Broadcast::routes([ 'middleware' => 'auth:api' ]);

    我在前端添加了授权令牌-

     auth: {
        headers:
        {
            'Authorization': 'Bearer ' + accToken,
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2017-01-10
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 2022-01-25
      • 2020-02-18
      相关资源
      最近更新 更多