Laravel广播身份验证工作或网络警卫或自定义警卫

时间:2020-05-28 14:00:07

标签: php laravel

在项目中,我有两个身份验证卫士,即Web(默认情况下为用户)和我的自定义(老师)。我注意到,老师不能授权广播。在Broadcast::routes(['middleware' => ['web', 'auth:teacher']])中添加中间件后,教师已成功授权,但用户正在重定向到登录页面。 所以我有以下问题:

  1. 具有Broadcast::routes();代码,该广播只能正常工作 对于用户看守,对于教师,则返回403 forbidden错误。

  2. 具有Broadcast::routes(['middleware' => ['web', 'auth:teacher']]);代码,该广播仅对教师有效 保护,因为用户将重定向到auth,登录名和用户页面。

config / auth.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'teacher' => [
            'driver' => 'session',
            'provider' => 'teacher',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'teacher' => [
            'driver' => 'eloquent',
            'model' => App\Teacher::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

app \ Providers \ BroadcastServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {   
        Broadcast::routes(['middleware' => ['web', 'auth:teacher']]);

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

1 个答案:

答案 0 :(得分:2)

这来晚了,但是,如果您还没有 Broadcast::routes(['middleware' => ['auth:web', 'auth:teacher']]);

,则应该尝试一下

Broadcast::routes()适用于学生,因为使用了身份验证中间件的默认“ auth:web”保护,并且当您为教师添加时,您指定了错误的“ web”,而不是“ auth:web”。 尝试使用 Broadcast::routes(['middleware' => ['auth:web', 'auth:teacher']]);Broadcast::routes(['middleware' => ['auth:web,teacher']]);