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(); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验