Php Laravel/broadcasting/auth总是失败,出现403错误
我最近深入研究了Laravel5.3的LaravelEcho和Pusher组合。我已经成功地建立了公共频道,并转到了私人频道。我在Laravel从/broadcasting/auth路径返回403时遇到问题,无论我如何尝试授权该操作(包括使用简单的return true语句)。谁能告诉我我做错了什么 App/Providers/BroadcastServiceProvider.php:Php Laravel/broadcasting/auth总是失败,出现403错误,php,laravel,pusher,laravel-echo,Php,Laravel,Pusher,Laravel Echo,我最近深入研究了Laravel5.3的LaravelEcho和Pusher组合。我已经成功地建立了公共频道,并转到了私人频道。我在Laravel从/broadcasting/auth路径返回403时遇到问题,无论我如何尝试授权该操作(包括使用简单的return true语句)。谁能告诉我我做错了什么 App/Providers/BroadcastServiceProvider.php: namespace App\Providers; use Illuminate\Support\Servic
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;
});
}
}
resources/assets/js/booststrap.js:
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;
});
}
}
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调试控制台中看到事件及其有效负载,它一旦到达验证路径就会失败。对我有效的方法是使用Laravel Echo包的方法private:
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;
});
}
}
我通过创建通道路径来解决它 在routes->Channels.php中创建授权频道
Broadcast::channel('chatroom', function ($user) {
return $user;
});
见文件:
谢谢在我的案例中,问题是错误的用户id:
Echo.private('user.'+CURRENT_USER_ID_HERE)
如果您不再登录,则可能发生这种情况。确保您已实际登录Laravel应用程序,并且当前会话尚未过期
我重新登录,它为我工作。错误403/broadcasting/auth with Laravel version>5.3&Pusher,您需要使用更改resources/assets/js/bootstrap.js中的代码
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your key',
cluster: 'your cluster',
encrypted: true,
auth: {
headers: {
Authorization: 'Bearer ' + YourTokenLogin
},
},
});
以及在app/Providers/BroadcastServiceProvider.php中更改
Broadcast::routes()
与
或
它对我很有效,希望能对您有所帮助。如果有人在最新的Laravel 5.7中遇到同样的问题,那么对我有效的解决方案是在返回前或返回时检查每个通道的身份验证,如下所示
Broadcast::channel('user.*', function ($user) {
return Auth::check();
});
Broadcast::channel('conversation.{id}', function ($user, $conversationId) {
Auth::check();
return $user->isInConversation(Conversation::find($conversationId));
});
通过这种方式,它可以与任何频道一起播放任何事件,包括用户
我希望它可以帮助其他人。检查您是如何授权您的频道的。根据您的设置,这可能会有所帮助。使用以下内容更新您的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(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
}
我将socket.io与redis配对,并且还遇到了403错误问题,尽管没有通过/broadcasting/auth路由的任何身份验证中间件。只有当我发现仅仅通过通道授权是不够的时,才应该有用户,无论您如何使用默认的laravel auth或某种令牌算法(jwt或其他)来验证和获取用户
经过身份验证的用户将自动解析并作为第一个参数传递给routes/channels.php文件中的closures函数,因此您可以检查当前登录用户的通道可用性
这仅在您使用API令牌时才相关。您链接到的表示通道方法的文档应包括。。。返回true或false的回调…,那么为什么返回$user
?@Don'tPanic,因为对于状态频道,您应该返回有关用户的信息,而不是boolean@MikelGranero谢谢,我现在看到了这一部分的描述。OP的问题是关于私人频道AFAICT,而不是状态,所以我不确定这个答案有什么帮助,但我学到了一些东西:-)这给了我一个302(已找到)状态:/You save me:)。谢谢。代码转储不是好答案。您应该解释这是如何以及为什么解决他们的问题的。我建议你读书。”
<?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(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
}
-- reinstalling websockets
-- php artisan optimize:clear