php - Laravel 广播 auth 路由简单地返回 "true"

标签 php laravel laravel-5.3 pusher

我在 Laravel 5.3 中设置并初始化了我的按键。当我在我的本地环境中测试它时,它有效。当我尝试在我们的生产环境中运行完全相同的代码时,出现此错误:

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Auth info required to subscribe to private-App.User.16"}}}

我已确认 Pusher key 在我的本地和生产环境中是相同的。

WS 在两个环境中初始化相同:

wss://ws.pusherapp.com/app/264P9d412196d622od64d?protocol=7&client=js&version=4.1.0&flash=false

我能看到的唯一区别是,当我们的生产服务器联系 Laravel 的“broadcasting/auth”路由时,它只是在响应正文中接收到 true

当我的本地联系人“广播/授权”时,它会在响应中得到这个:

{auth: "22459d41299d6228d64d:df5d393fe37df0k3832fa5556098307f145d7e483c07974d8e7b2609200483f8"}

在我的 BroadcastServiceProvider.php 中:

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

    // Authenticate the user's personal channel.
    Broadcast::channel('App.User.*', function (User $user, $user_id) {
        return (int)$user->id === (int)$user_id;
    });
}

什么会导致 broadcast/auth 路由返回简单的 true 而不是预期的 auth?

最佳答案

如果您检查 PusherBroadcaster.php 文件,您会看到响应可以是“混合的”。

我认为文档只是在谈论默认广播。

The channel method accepts two arguments: the name of the channel and a callback which returns true or false indicating whether the user is authorized to listen on the channel.

这是 PusherBroadcast 中的 validAuthenticationResponse 方法。

/**
 * Return the valid authentication response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $result
 * @return mixed
 */
public function validAuthenticationResponse($request, $result)
{
    if (Str::startsWith($request->channel_name, 'private')) {
        return $this->decodePusherResponse(
            $this->pusher->socket_auth($request->channel_name, $request->socket_id)
        );
    }

    return $this->decodePusherResponse(
        $this->pusher->presence_auth(
            $request->channel_name, $request->socket_id, $request->user()->getAuthIdentifier(), $result)
    );
}

再举一个例子,这是在 RedisBroadcast 中。

if (is_bool($result)) {
    return json_encode($result);
}

关于这个“授权请求”的简短解释:

BroadcastManager 实例化所有“可用驱动程序”(Pusher、Redis、Log 等),并创建“auth”路由(使用 BroadcastController + 身份验证方法)。

当你调用“auth”时,会发生这样的事情:

  1. 调用“broadc.../auth”路由。
  2. BroadcastManager 将实例化适当的驱动程序(在您的情况下为 Pusher)
  3. PusherBroadcaster 可以抛出异常 AccessDeniedHttpException 如果用户未通过身份验证(“用户 session ” - Auth::user() 未定义/为空)并且是尝试访问私有(private)(或存在) channel 类型。
  4. 如果用户试图访问一个private/presence channel 并且用户已经过身份验证(Auth::check()),Laravel 将检查是否授权。用户可以访问该 channel 。 (检查:verifyUserCanAccessChannel 方法)。
  5. 之后,将调用validAuthenticationResponse 方法。此方法将使用用户凭据向推送器发出请求并返回一个数组。此数组包含 Pusher 响应(套接字身份验证:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L586/Presence Auth:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L615),它是一个字符串。

简答:

Soo.. Pusher 需要这个授权响应。否则您将无法连接/识别用户(wss://ws.pusherapp.com....)。

关于php - Laravel 广播 auth 路由简单地返回 "true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361839/

相关文章:

php - 在非 laravel 项目的 Laravel 中使用 $_SESSION

php - 如何更新 Laravel 中表内列的数据类型?

php - Laravel [5.3|5.4] 测试作业中的作业调度

php - 从查询转换为 Yii2 的 ModelSearch

PHP cron 作业未通过 file_put_contents() 运行日志

php - 我的 PHP 代码不工作?

javascript - 为什么分页链接点击后返回json? (Vue.js 2)

php - json解码删除新行

php - Laravel 5 Socialite Facebook 登录用户取消应用程序请求后回调处理错误

mysql - 从温度和 ID 数据库创建 Google Charts API 数据表