首页 > 其他分享 > Laravel — P53:速率限制

Laravel — P53:速率限制

时间:2023-02-12 14:34:23浏览次数:44  
标签:Laravel Illuminate Middleware api 速率 Http throttle class P53

对于眼尖的读者,您可能已经注意到我们上一篇文章中调用​​configureRateLimiting​​​的方法。​​RouteServiceProvider​

protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
}

正如您在代码中看到的,速率限制器是在​​RateLimiter​​​外观的​​for​​​方法中定义的。它接受一个速率限制器和一个返回限制配置的闭包。在这种情况下,这是路由的速率限制器​​api​​,我们尚未介绍。

protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});

RateLimiter::for('web', function (Request $request) {
return Limit::perMinute(5)->by($request->user()?->id ?: $request->ip());
});
}
Route::middleware(['throttle:web'])->group(function () {
Route::get('/throttle-test-1', function() {
return "Throttle Test 1";
});

Route::get('/throttle-test-2', function() {
return "Throttle Test 2";
});
});

  Laravel — P53:速率限制_php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array<string, class-string|string>
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $middlewareGroups = [
'web' => [
'throttle:web',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

标签:Laravel,Illuminate,Middleware,api,速率,Http,throttle,class,P53
From: https://blog.51cto.com/u_1213352/6052058

相关文章

  • Laravel — P52:显式路由模型绑定
    什么时候需要显式绑定模型与使用Laravel的隐式绑定?我经常发现的一个常见问题没有适当或足够简单的回答。因此,我认为它没有像应有的那样频繁使用。在前面的示例中,我们使用......
  • Laravel — P51:路由模型隐式绑定
    现在我们已经熟悉了依赖注入,我们还能用这个概念做什么呢?我们能否像模型一样将我们的模型“注入”​​User​​​到我们的路由中,然后在不实例化​​User​​模型的情况下利用......
  • Laravel — P50:路由和依赖注入
    我们在前几篇文章中介绍了一些依赖注入主题,但现在我们想开始更具体一些。我们终于进入了Laravel的更高级部分,并将开始关注Laravel特定领域内的特定主题。接下来的几篇文......
  • Laravel — P49:依赖注入
    我希望上一篇文章有意义。依赖注入确实是一个简单的概念。我觉得在你说你知道“Laravel中的依赖注入”之前,你应该完整地理解它。在我们再次进入依赖注入之前,你应该了解Lar......
  • Laravel — P48:依赖注入概念(高级 PHP 概念)
    在你的Laravel职业生涯中,你会多次听到“依赖注入”这个词,所以你不妨熟悉一下它。在我们进入Laravel之前,让我们看看依赖注入作为PHP(或任何编程语言)中的一个概念。我们应......
  • Laravel — P55:路由中的软删除模型
    完成这样的教程系列并不像看起来那么简单。您想按特定顺序涵盖主题,但每次谈到一个新主题时,您都会意识到您需要涵盖其他内容才能做到这一点。例如,在学习了Laravel中一些比......
  • [洛谷P5368] 真实排名
    [PKUSC2018]真实排名题目描述小C是某知名比赛的组织者,该比赛一共有\(n\)名选手参加,每个选手的成绩是一个非负整数,定义一个选手的排名是:成绩不小于他的选手的数量(包括......
  • laravel groupby 报错
    报错信息laravelwhichisnotfunctionallydependentoncolumnsinGROUPBYclause;thisisincompatiblewithsql_mode=only_full_group_by关闭严格模式:修改app......
  • laravel data_get 函数从多维数组中获取值
    当需要从多维数组中获取值,并且进行判空,赋值默认值的时候,可以使用下面的测试用例。$arr=["name"=>"陶士涵","title"=>["sub"=>"测试"],"test"=>null......
  • Laravel配置全局公共函数的方法步骤
    app目录下创建一个名为helpers.php的文件(app/helpers.php)并编辑其内容如下app\helpers.php<?php//全局函数/***三个一组相加*/if(!function_exists('sgsyz')){f......