1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel レートリミットについて

Last updated at Posted at 2023-10-12

概要

  • laravelのレートリミットについて簡単にまとめる。

レートリミットについて

  • laravelには「ルーティングに対するトラフィック量を制限」する機能が備わっている。これがいわゆるレートリミットである。

  • 悪意を持ったユーザーや、サーバー負荷軽減の観点からレートリミットは適切に設定する必要がある。

  • デフォルト状態でレートリミットは下記で設定されている。

    • app/Providers/RouteServiceProvider.php
  • configureRateLimitingというメソッドで設定される。

    app/Providers/RouteServiceProvider.php
    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }
    
  • 上記はデフォルトでapiという名前のレートリミットが1分間に60回までの同ユーザーID or 同IPアドレスからのリクエストを許容するように設定されている。

  • 上記のレートリミットは下記でapi.phpのルーティングファイルに記載されるすべてのルートに適応されている。

    • app/Http/Kernel.php
  • $middlewareGroupesという配列のapiのキーで紐付けられている。(下記の\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',の部分)

    app/Http/Kernel.php
    
    /**
     * 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,
            \Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
    

参考文献

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?