laravel-5 - laravel 私人频道和 laravel-echo-server 的身份验证问题
问题描述
我正在使用 laravel-echo-server 以及 Redis 和 vuejs 通过 Laravel 5.5 中的 websockets 广播事件。使用公共频道,它工作正常,事件可以正确广播到客户端。但是,当我将其更改为私有通道时,即使 channel.php 文件中的回调函数只返回 true 并且不包含任何身份验证逻辑,我也会面临身份验证问题。我正在使用 Sentinel 身份验证包,我不知道这是否是问题所在。但正如我所说,当只是返回“真”时,身份验证问题仍然存在。
当我检查 laravel-echo-server 时,我看到有一条错误消息显示“无法通过身份验证,获得了 http 代码 419”。
我读到有些人面临同样的问题,他们得到的可能是 csrf-token 问题或私人频道仅适用于集成的身份验证包......等等。
我在 window.Echo 配置中包含了 csrf-token 标头,但没有结果。
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: {
headers: {
'X-CSRF-TOKEN': $('meta[name=csrf-token]).attr(content)
}
}
});
路线/通道.php
<?php
Broadcast::channel('adminNotify', function() {
return true;
});
为了使 id 为 1 的管理员能够接收事件,我实际上所做的是:
<?php
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
Broadcast::channel('adminNotify', function () {
return (int) Sentinel::check()->id === 1;
});
应用程序\事件\通知管理员事件
<?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\Ad;
use Sentinel;
class NotifyAdminEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $ad;
public function __construct(Ad $ad)
{
$this->ad = $ad;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('adminNotify');
}
}
应用程序.js
require('./bootstrap');
window.Vue = require('vue');
import Echo from 'laravel-echo';
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
laravel-echo-server.json
{
"authHost": "http://192.168.10.11",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "64fe4c095b0ffb30",
"key": "9e2bf37f9b3c8c88c3c5f5e207754f1d"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
Laravel log 没有记录任何东西,所以我发现很难知道发生了什么并解决问题。
解决方案
我已经弄清楚发生了什么。问题出在laravel-echo-server.json文件中,并且恰好在第一行“authHost”中,而不是放置ip我不得不放置domaine name。
{
"authHost": "http://webdev-app.test",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "64fe4c095b0ffb30",
"key": "9e2bf37f9b3c8c88c3c5f5e207754f1d"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
推荐阅读
- python - ValueError: Columns must be the same length as key ---- 为什么会这样?
- r - 通过根据两个定性因素组合相似的行来减少数据框
- ajax - 当您更改价格时,Laravel Ajax 购物车会动态更改小计
- vmware - 无法在 Cloud Foundry 中创建共享域 - 504 网关超时
- snowflake-cloud-data-platform - 插入 TABLE($TABLE_VARIABLE) 雪花
- c - 为什么我的strcmp的值不等于0或者无效?
- android - 带有 Material Outline Box 的 boxStroke 不起作用
- r - R:在特定字符串之后提取数字
- twilio - Twilio 函数检索语音邮件类型错误:不推荐使用参数“auth”。改用“用户名”/“密码”
- r - 如何在一个几何而不是另一个上绘制网格