将自定义中间件添加到 Laravel Passport 端点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45497309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:27:04  来源:igfitidea点击:

Add custom middleware to Laravel Passport endpoints

phplaravelroutesmiddlewarelaravel-passport

提问by fatuous.logic

I have a standard Laravel Passport setup on 5.4 - it all works fine and is generating tokens.

我在 5.4 上有一个标准的 Laravel Passport 设置 - 一切正常并且正在生成令牌。

I protect my API routes using the auth:api middleware as well as a custom middleware that checks that specific headers in a request are present and valid before any requests are handled. This middleware works fine for the API routes group.

我使用 auth:api 中间件以及自定义中间件来保护我的 API 路由,该中间件在处理任何请求之前检查请求中的特定标头是否存在且有效。这个中间件适用于 API 路由组。

Is there a way to wrap the Passport routes generated by laravel '.../oauth/token' in this middleware as well?

有没有办法在这个中间件中包装由 laravel '.../oauth/token' 生成的 Passport 路由?

Currently I have set up the routes in my AuthServiceProvider.php boot() method:

目前我已经在我的 AuthServiceProvider.php boot() 方法中设置了路由:

public function boot()
{
    $this->registerPolicies();

    // Passport/OAuth
    Passport::routes(function ($router) {
      $router->forAccessTokens();
      $router->forTransientTokens();
    });

    Passport::tokensExpireIn(Carbon::now()->addDays(7));

    Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}

The end goal is that the oauth endpoints will return an error if the headers are not present.

最终目标是如果标头不存在,oauth 端点将返回错误。

采纳答案by Varin

If you only need to add middleware to one Passport route for example /oauth/token, you can do it this way:

例如/oauth/token,如果您只需要向一个 Passport 路由添加中间件,您可以这样做:

  1. Look up the route you need by typing php artisan r:l
  2. Check the controller and method used for this route, in out example it is going to be AccessTokenController@issueToken
  3. Create the controller that extends AccessTokenController, you can leave it empty
  1. 通过键入查找您需要的路线 php artisan r:l
  2. 检查用于此路由的控制器和方法,在示例中它将是 AccessTokenController@issueToken
  3. 创建扩展AccessTokenController的控制器,可以留空
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Passport\Http\Controllers\AccessTokenController;

class ApiTokenController extends AccessTokenController
{

}
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Passport\Http\Controllers\AccessTokenController;

class ApiTokenController extends AccessTokenController
{

}
  1. Then create a route to that controller and method (as this controller inherits all the parent controller methods):
  1. 然后创建到该控制器和方法的路由(因为该控制器继承了所有父控制器方法):

Route::middleware('MyMiddleware')->post('/api-token', 'ApiTokenController@issueToken');

Route::middleware('MyMiddleware')->post('/api-token', 'ApiTokenController@issueToken');

回答by rdehnhardt

You can try this: Go to app/Providers/AuthServiceProviderand look for the function boot(). In this function you will see a line for registering routes for Passport. The default code is Passport::routes(). This routes()method accepts an options array as second argument. You can use it to set middlewares for Passport routes.

你可以试试这个:去app/Providers/AuthServiceProvider寻找function boot(). 在此功能中,您将看到一行用于注册 Passport 的路线。默认代码是Passport::routes()。此routes()方法接受一个选项数组作为第二个参数。您可以使用它为 Passport 路由设置中间件。

Passport::routes(null, ['middleware' => 'api']);

回答by Martin Joiner

In the app/Providers/AuthServiceProviderinclude the Route facade by adding this use statement somewhere in the top:

app/Providers/AuthServiceProvider包括路由门面加在上面这种使用声明的地方:

use Illuminate\Support\Facades\Route;

Then on the boot()method, put the Passport::routes() inside a Route::group() like this:

然后在boot()方法上,将 Passport::routes() 放在 Route::group() 中,如下所示:

Route::group(['middleware'=>'MyFunkyCustomMiddleware'], function(){
    Passport::routes(); // <-- Replace this with your own version
});

Hope that helps!

希望有帮助!