如何更改Laravel的默认广播身份验证中间件

问题描述 投票:1回答:2

因此,正如我的标题所说,我想将Laravel的默认Broadcast auth中间件更改为我使用基于令牌的身份验证制作的自定义身份验证中间件。我之所以这样做是因为我的应用程序是一个基于API的应用程序,当用户进行身份验证时,我会创建一个会话令牌并将其发送给他,并使用expires_at列将其存储在数据库中。我正在使用Pusher

我有以下中间件:

class AuthCustom
{
    public function handle($request, Closure $next)
    {
        // if we have the session token stored in header
        if ($request->header('x-session')) {
            $session = Session::where('id', $request->header('x-session'))->where('expires_on', '>=', date('Y-m-d G:i:s'))->with('user')->first();
            if ($session !== null) {
                $user = (new User())->where('id', $session->user_id)->first();
                if ($user !== null) {
                    $request->merge(['user' => $user]);

                    return $next($request);
                }
            }
        }
}

我的BroadcastServiceProvider代码如下:

class BroadcastServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Broadcast::routes();

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

如果我把Broadcast::routes(['middleware' => 'authcustom']);放在BroadcastServiceProviderboradcasting/auth给出403状态代码,因为$request->user()为null,然后导致Access forbidden

我试过搜索整个该死的网络,但我没有发现更改默认的auth中间件进行广播。

我甚至尝试删除Broadcast::routes()并定制一个新路线/broadcast返回Pusher socket_auth对象,每次我得到一个419 Unkown状态代码。

任何想法,或者你可以指出我可以管理这个的方向?谢谢!

稍后编辑:我的JS Echo连接如下所示:

Vue.use(VueEcho, {
    broadcaster: 'pusher',
    key: 'xxxxxxxxxxxxxx',
    cluster: 'eu',
    authEndpoint: 'http://localhost/api.easycargo.ro/public/broadcasting/auth',
    auth: {
        headers: {
            'x-session': this.auth.token
        }
    }
});
php laravel broadcast laravel-5.6 pusher
2个回答
3
投票

我很高兴你有一些工作。对于后来的读者来说,这里有一个更为Laravel式的解决问题的方法:创建一个用于验证特殊路径请求的custom auth guard

Laravel的AuthManager包含一个辅助方法-viaRequest(),它简化了Guard的创建,该Illuminate\Contracts\Auth\Guard使用来自请求上下文的数据对用户进行身份验证,而无需完全实现boot()。我们可以在AuthServiceProvider.php中的public function boot() { Auth::viaRequest('custom-auth', function ($request) { // Any custom user-lookup logic here. For example: if ($request->header('x-session')) { $user = // ...try to fetch a user... return $user; } }); } 方法中绑定我们的自定义保护:

viaRequest()

我们可以看到,我们只是将一个闭包传递给User方法,该方法在身份验证成功时返回null对象,或者在身份验证失败时返回'guards'

接下来,我们将通过在config / auth.php中向'guards' => [ ... 'broadcasting' => [ 'driver' => 'custom-auth', ], ], 数组添加一个条目来告诉Laravel我们的新auth guard:

Guard

最后,我们需要为应该使用我们的自定义middleware parameter验证用户的任何路由更新中间件。我们可以使用Laravel的内置auth中间件,并指定哪个后卫应用为Broadcast::routes([ 'middleware' => [ 'auth:broadcasting', ... ] ]); 。例如,我们将在问题的BroadcastServiceProvider.php中初始化广播路由:

broadcasting

...其中Guard与我们在config / auth.php中分配给我们自定义Auth的名称相匹配。

这种方法使我们能够使用Laravel的所有$user服务,提供更加集中的地方来定义我们的身份验证逻辑,并简化自动化测试,因为我们可以根据需要更轻松地模拟身份验证。


1
投票

我实际上设法找到了解决方案,所以我需要做的就是通过执行以下操作将我在自定义auth中间件中获得的$request->merge(['user' => $user]); //add this $request->setUserResolver(function () use ($user) { return $user; }); 绑定到请求:

$request->user()

现在qazxswpoi laravel检查返回用户对象并通过验证。

© www.soinside.com 2019 - 2024. All rights reserved.