delaney/superban

根据所选时间段内的请求数量,完全禁止客户端一段时间

dev-main 2023-12-22 04:11 UTC

This package is auto-updated.

Last update: 2024-09-22 05:38:27 UTC


README

当客户端在设定时间内发送过多请求时,禁止它们在API中一段时间。

安装

在您的Laravel应用程序中,使用Composer安装软件包并发布配置文件

composer require delaney\superban

配置与使用

使用此命令发布配置文件

php artisan vendor:publish --tag=superban

config\superban

/*
 * Config file for the Superban package.
 */

return [
    /*
    |--------------------------------------------------------------------------
    | Superban - Cache Stores
    |--------------------------------------------------------------------------
    |
    | This is an array of cache stores to use with Superban.
    | Leaving this as empty would use the default cache store.
    |
    | Example:
    | ['redis', 'file']
    |
    */
    'drivers' => [],

    /*
    |--------------------------------------------------------------------------
    | Superban - Ban IP Addresses
    |--------------------------------------------------------------------------
    |
    | Add the IP address when banning a client.
    |
    */
    'ban_ip_addresses' => env('SUPERBAN_BAN_IP', true),

    /*
    |--------------------------------------------------------------------------
    | Superban - User Keys
    |--------------------------------------------------------------------------
    |
    | This is a list of keys to ban the client user.
    | These keys should be available on your user model.
    |
    */
    'user_keys' => ['id', 'email'],

    /*
    |--------------------------------------------------------------------------
    | Superban - Message
    |--------------------------------------------------------------------------
    |
    | Message to display to a banned client in the HTTP Response
    |
    | Default: "You have been banned temporarily."
    |
    */
    'message' => env('SUPERBAN_ERROR_MESSAGE'),
];

您可以使用以下方式在 .env 文件中指定选项

SUPERBAN_BAN_IP=false
SUPERBAN_ERROR_MESSAGE="This request is banned"

使用中间件

要将Superban添加到路由中,只需像使用任何其他中间件一样使用它,使用别名 superban 并指定您的参数,时间以分钟为单位。例如

Route::post('clear')->middleware('superban:100,60,1440');

Route::prefix('/resources')->middleware('superban:20,1,10')
    ->group(function() {
        Route::get() ...

在第一行中,1小时内(60分钟)发送100个请求的用户将被禁止24小时(1440)。在第二个中间件组中,1分钟内超过20个请求的用户将被禁止10分钟。

许可证

MIT