0
0

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 3 years have passed since last update.

【Laravel】レート制限機能を使ってセキュリティを向上させる

Posted at

ルート定義ファイルroutes/web.phpにレート制限をかけるミドルウェアを適応させて、セキュリティの向上を図りたいと思います。アクセスの回数制限を設定したい場合に有効です。

レート制限の書き方

Route::middlewareメソッドにthrottleミドルウェアを指定してください。このthrottleミドルウェアは「時間内に許す最大リクエスト数」と「時間」の2つを指定します。

routes/web.php
Route::middleware('auth:api', 'throttle:60,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});

上記の例のコードでは、1分間に60回のアクセスを許可するルートになります。

動的レート制限の書き方

もし、認証済みのモデルの中に、最大リクエスト属性がある場合には、その値を動的に最大リクエスト数に適応することもできます。

routes/web.php
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
    Route::get('/user', function () {
        //
    });
});

例えば、認証したモデルにrate_limit属性があり、値を持っている場合、その値がthrottleミドルウェアのrate_limitに渡されるという感じです。

参考文献:https://readouble.com/laravel/7.x/ja/routing.html

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?