Laravel Guard

Laravel Guard

Guard我的理解他应该是一个类似用户认证的东西。

config/auth.php 中有配置 guards 的参数,可以看出来 webapi 是两个 guards
在平时的业务中api可能更多的是用户前台用户的操作,而web更多的是后台用户的操作。
默认配置都是指向 usersprovider

 

/*
    | 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"
    |可选驱动:"session", "token"
    */
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

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

接下来看看 provider 都有什么配置。

 

  /*
    | User Providers
    |用户提供者
    | 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,
        ],

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

providers 中有刚才看到的 users 配置了吧,这个就是配置laravel的用户组,因为前台和后台用户在一般的情况下是分开操作的。所以就可以在这里新建一个 admin 的用户组,并配置。开箱默认只用 users

相对的每个用户组密码操作应该也是不同的,所以这里还有 password 的配置。

 

  /*
    | 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,
        ],
    ],

平时用于检测登陆用户,或者用户是否登陆我们都用的是 Auth::check() 就可以检测到,这是因为在使用默认配置的时候,guard自动配置为 users 用户组。

 

  /*
    | 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',
    ],

web guard下指向的就是 users 模型。
但是当如果我们有两个用户组,前台后台的时候怎么进行验证呢?

Auth::check() 是判断用户是否登录的方法,如果使用的默认用户系统,那这样使用没问题。

但是使用两组用户的话,就应该这样操作:
Auth::guard('api')->check() 就是用来判断前台用户是否登录 Auth::guard('web')->check() 就是用来判断后台用户是否登录

所以如果使用非默认用户组,则需要用guard来指定。

所以我们平时用的其实就是默认配置,写全就应该是 Auth::guard(‘web)->check()



作者:禹声
链接:https://www.jianshu.com/p/c6bde998b87b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章