Jwt-auth 多表验证

使用 jwt 有时需要两个表的权限认证,users表和vips表。

配置多个 guard

config/auth.php

'defaults' => [
    'guard'     => 'user', //默认guard
    'passwords' => 'users',
],
'guards' => [
    'user'   => [ //jwt guard 1
        'driver'   => 'jwt',
        'provider' => 'users',
    ],
    'vip'   => [ //jwt guard 2
        'driver'   => 'jwt',
        'provider' => 'vips',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model'  => App\Model\User::class,
    ],
    'vips' => [
        'driver' => 'eloquent',
        'model'  => App\Model\Vip::class,
    ],
],

创建更改默认 guard 的中间件

<?php
namespace App\Http\Middleware;
use Closure;
class SetDefaultGuard
{
    public function handle($request, Closure $next, $guard)
    {
        config(['auth.defaults.guard'=>$guard]);
        return $next($request);
    }
}

添加中间件

Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        ...
    ],
    'user' => [
        'throttle:60,1',
        'bindings',
    ],
    'vip' => [
        'throttle:60,1',
        'bindings',
    ],
];
protected $routeMiddleware = [
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'setguard'  => \App\Http\Middleware\SetDefaultGuard::class, // 这里
    'auth.user' => \App\Http\Middleware\UserAuth::class, // 权限认证中间件1
    'auth.vip' => \App\Http\Middleware\VipAuth::class,// 权限认证中间件2

    // 'auth'     => \Illuminate\Auth\Middleware\Authenticate::class,
    //'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    //'can'      => \Illuminate\Auth\Middleware\Authorize::class,
    //'guest'       => \App\Http\Middleware\RedirectIfAuthenticated::class,
];

在权限认证中间件之前使用这个中间件

App\Providers\RouteServiceProvider

public function map()
{
    $this->mapUserRoutes(); //1
    $this->mapVipRoutes(); //2
    $this->mapAdminRoutes();
    $this->mapWebRoutes();
}
...
protected function mapVipRoutes()
{
    // 在权限认证中间件之前,使用这个中间件
    Route::middleware(['setguard:vip','vip'])->prefix('vip')->namespace($this->namespace . '\Vip\\')->group(base_path('routes/vip.php'));
}

测试

这时在两个不同的权限认证中间件中打印 auth()

user

<?php
namespace App\Http\Middleware;
...
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
class UserAuth extends BaseMiddleware
{
    public function handle($request, Closure $next)
    {
        dd( auth() ); //这里是默认 guard
        dd( $this->auth );
    }
}

Jwt-auth 多表验证

vip

<?php
namespace App\Http\Middleware;
...
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
class VipAuth extends BaseMiddleware
{
    public function handle($request, Closure $next)
    {
        dd( auth() ); //验证这里是否更改了默认guard
        dd( $this->auth );// JWTAuth对象已切换到 vip guard 读取的是vips表
    }
}

Jwt-auth 多表验证

本作品采用《CC 协议》,转载必须注明作者和本文链接

简洁略带风骚

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!