Laravel /broadcasting/auth 总是因 403 错误而失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41728930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:09:42  来源:igfitidea点击:

Laravel /broadcasting/auth Always Fails With 403 Error

phplaravelpusherlaravel-echo

提问by user2321275

I have recently delved into Laravel 5.3's Laravel-Echo and Pusher combination. I have successfully set up public channels and moved on to private ones. I am having trouble with Laravel returning a 403 from the /broadcasting/auth route, no matter what I do to try to authorize the action (up to and including using a simple return true statement). Can anyone tell me what I am doing wrong?

我最近深入研究了 Laravel 5.3 的 Laravel-Echo 和 Pusher 组合。我已经成功建立了公共频道并转向了私人频道。我遇到了 Laravel 从 /broadcasting/auth 路由返回 403 的问题,无论我做什么尝试授权操作(最多并包括使用简单的 return true 语句)。谁能告诉我我做错了什么?

App/Providers/BroadcastServiceProvider.php:

应用程序/Providers/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();

        /*
         * Authenticate the user's personal channel...
         */
        Broadcast::channel('App.User.*', function ($user, $userId) {
            return true;
        });
    }
}

resources/assets/js/booststrap.js:

资源/资产/js/booststrap.js:

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);
    });

I can see the event and it's payload in my Pusher debug console, it is simply failing once it hits the auth route.

我可以在我的 Pusher 调试控制台中看到该事件及其有效负载,一旦它到达 auth 路由,它就会失败。

回答by Alex

Error 403 /broadcasting/auth with Laravel version > 5.3 & Pusher, you need change your code in resources/assets/js/bootstrap.js with

错误 403 /broadcasting/auth 与 Laravel 版本 > 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
        },
    },
});

And in app/Providers/BroadcastServiceProvider.php change by

在 app/Providers/BroadcastServiceProvider.php 中更改为

Broadcast::routes()

with

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

or

或者

Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT

it worked for me, and hope it help you.

它对我有用,希望对你有帮助。

回答by Jakhongir

I paired socket.io with redis and also had a problem with 403 error, even though there weren't any authentication middlewares over /broadcasting/auth route. Only after whatching laracasts lessonI figured out that just channel authorization is not enough, there always should be user and no matter how you authenticate and obtain user, using default laravel auth or some token algorithm - jwt or anything else.

我将 socket.io 与 redis 配对,并且也遇到了 403 错误的问题,即使 /broadcasting/auth 路由上没有任何身份验证中间件。只有在学习了laracasts 课程后,我才发现仅频道授权是不够的,始终应该有用户,无论您如何验证和获取用户,使用默认的 laravel auth 或某些令牌算法 - jwt 或其他任何东西。

Authenticated user is automatically resolved and passed as first parameter to to closures functions in routes/channels.php file, so you can check channel availability for currently logged in user enter image description here

经过身份验证的用户会自动解析并作为第一个参数传递给 routes/channels.php 文件中的闭包函数,因此您可以检查当前登录用户的频道可用性 在此处输入图片说明

回答by Juan Francisco Andrade

What worked for me was to use the method privateof the Laravel Echo package: https://laravel.com/docs/5.3/notifications#listening-for-notifications

对我有用的是使用Laravel Echo 包的私有方法:https://laravel.com/docs/5.3/notifications#listening-for-notifications

Echo.private('App.User.1')
  .notification((notification) => {
  console.log(notification.type);
});

回答by M Arfan

I solve it by creating channel route.

我通过创建通道路由来解决它。

Create your Authorizing Channels in routes->channels.php

在 routes->channels.php 中创建您的授权频道

Broadcast::channel('chatroom', function ($user) {
    return $user;
});

See Documentation : https://laravel.com/docs/5.4/broadcasting#authorizing-channels

请参阅文档:https: //laravel.com/docs/5.4/broadcasting#authorizing-channels

thanks

谢谢

回答by Ben F

Check how you are authorising your channel. Depending on your setup this might help. Update your BroadcastServiceProvider with the following:

检查您如何授权您的频道。根据您的设置,这可能会有所帮助。使用以下内容更新您的 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');
    }
}

Adds in the Auth API middleware for use with Laravel Passport.

添加用于 Laravel Passport 的 Auth API 中间件。

回答by Jacinto Joao

In case, someone comes in the latest Laravel 5.7 with the same issues, the good solution worked for me is to check authentication in each channel before returning or on the return like below.

如果有人在最​​新的 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));
});

This way it works with any channel broadcasting any event including users.

通过这种方式,它适用于任何广播任何事件的频道,包括用户。

I hope it may help someone else.

我希望它可以帮助别人。

回答by Nurbol Alpysbayev

In my case the problem was a wrong user id:

就我而言,问题是错误的用户 ID:

Echo.private('user.'+CURRENT_USER_ID_HERE)

回答by FerrisBueller

This can happen if you are no longer logged in. Make sure you are actually logged into the Laravel app and that your current session hasn't expired.

如果您不再登录,就会发生这种情况。请确保您确实已登录 Laravel 应用程序并且您当前的会话尚未过期。

I logged back in and it worked for me.

我重新登录,它对我有用。