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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog