duanmibei1929 2018-03-25 15:24
浏览 135
已采纳

Laravel Passport将中间件添加到默认的身份验证/令牌路由

Am trying to build a multi tennant application and switching between the databases in my middleware

So i have the following middleware

    public function handle($request, Closure $next)
{
    //check if request has the value of header
    $passedheader = $request->header('HTTP_X_APP_LOCATION');

    $headers = AppConnectionsModel::get()->pluck('header_val')->toArray();

    if(!isset($passedheader) || !in_array($passedheader,$headers)){
        return response('Invalid Request.', 403);
    }

    //get the actual header passed
    $connection = AppConnectionsModel::where('header_val',$passedheader)->first();
    if($connection){
        Config::set('database.connections.tennant.database', $connection->database);
        Config::set('database.connections.tennant.port', $connection->port);
        Config::set('database.connections.tennant.username', $connection->username);
        Config::set('database.connections.tennant.password', $connection->password);
        Config::set('database.connections.tennant.host', $connection->host);


       return $next($request);

    }else{
        return response("Invalid Request.", 200);
    }

    return $next($request);
}

So what basically the middleware does is to check the connection settings and set to the config

So i would like to apply this middleware to the passport token route that is oauth/token so that every time a user sends a request to get a token his or her db is used

In my users model i have

protected $connection = "tennant";
protected $table = "tbl_user";

How do i override the oauth/token passport route in laravel to use this middleware

As suggested below i have also tried changing middleware priority in kernel file like

  protected $routeMiddleware = [
    'appconnection' => \App\Http\Middleware\AppConnectionMiddleware::class,
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,

];

app connection is the middleware io have placed at the top having the handle method shown above But it doesnt get executed before passport oauth/token route

Checking on my configs the passport routes are set in the AuthServiceProvider like

 class AuthServiceProvider extends ServiceProvider
  {
  protected $policies = [
      'App\Model' => 'App\Policies\ModelPolicy',
  ];

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();
    Passport::routes();
    //
}

}

  • 写回答

1条回答 默认 最新

  • dongxun8189 2018-03-25 15:34
    关注

    you need to add one array in Kernel.php file.

    protected $middlewarePriority = [
        \App\Http\Middleware\AppConnectionMiddleware::class,
        \Laravel\Passport\Http\Middleware\CheckClientCredentials::class
    ];
    

    Please do this and see, it should work.

    passport routes

    Route::group(['middleware'=>'appconnection'], function(){ 
        Passport::routes(); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算