当前位置:网站首页>Thinkphp6.0 middleware with limited access frequency think throttle
Thinkphp6.0 middleware with limited access frequency think throttle
2022-07-04 05:29:00 【Yuan rises and falls】
effect
The middleware can limit the number of visits of users in a period of time , It can be used to protect the interface from climbing and explosion .
install
composer require topthink/think-throttle After installation, the project will be automatically generated conf/throttle.php The configuration file , Components are not automatically enabled after installation , Manual setting required .
Turn on
Components work as middleware , Therefore, its startup is the same as other middleware , For example, in global middleware app/middleware.php :
<?php
return [
\think\middleware\Throttle::class,
];Configuration instructions
stay config/throttle.php configuration option :
<?php
// Middleware configuration
return [
// Cache key prefix , Prevent key values from conflicting with other applications
'prefix' => 'throttle_',
// The cache keys ,true Indicates the source of use ip
'key' => true,
// The type of request to be restricted , eg: GET POST PUT DELETE HEAD
'visit_method' => ['GET'],
// Set access frequency , for example '10/m' It means allowing requests per minute 10 Time . value null Means unrestricted , eg: null 10/m 20/h 300/d 200/300
'visit_rate' => '100/m',
// Response returned when access is restricted
'visit_fail_response' => function (Throttle $throttle, Request $request, int $wait_seconds) {
return Response::create('Too many requests, try again after ' . $wait_seconds . ' seconds.')->code(429);
},
];When the configuration item meets any of the following conditions , Access frequency will not be limited :
keyThe value isfalseornull;visit_rateThe value isnull.
among key The key used to set the cache ; and visit_rate Used to set the access frequency , The unit can be seconds , branch , when , God , for example :1/s, 10/m, 98/h, 100/d , It can also be 100/600 (600 Maximum in seconds 100 Requests ).
Flexible customization
Example 1 : Make restrictions on individual users , key The value of can be set as a function , This function returns the new cache key value ( need Session Support ), for example :
'key' => function($throttle, $request) {
$user_id = $request->session->get('user_id');
return $user_id ;
},Example 2 : You can also customize the generation for different controllers and methods in the callback function key, The middleware will transform :
'key' => function($throttle, $request) {
return '__CONTROLLER__/__ACTION__/__IP__';
},Or set it directly :
'key' => '__CONTROLLER__/__ACTION__/__IP__',PS: This example requires the middleware to be enabled after routing the middleware , In this way, the preset replacement function will take effect .
Example 3 : It is allowed to modify the current access frequency or temporarily change the current limiting policy within the closure :
'key' => function($throttle, $request) {
$throttle->setRate('5/m'); // Set the frequency
$throttle->setDriverClass(CounterSlider::class);// Set current limiting policy
return true;
},Example 4 : Allow independent configuration in route definition (1.3.x Versioning support )
Route::group(function() {
// Route registration
})->middleware(\think\middleware\Throttle::class, [
'visit_rate' => '20/m',
'key' => '__CONTROLLER__/__ACTION__/__IP__',
]);边栏推荐
- 2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
- (4) Canal multi instance use
- [matlab] matlab simulation - simulate the AM modulation process of the modulation system
- tutle时钟改进版
- 拓扑排序和关键路径的图形化显示
- Exercise bubble sort
- Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
- What is MQ?
- Simulated small root pile
- VB.net GIF(制作、拆解——优化代码,类库——5)
猜你喜欢

Principle and practice of common defects in RSA encryption application
![[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre](/img/3e/b5df691ca1790469eb1b4e8ea5b4c0.png)
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre

KMP match string

2022G2电站锅炉司炉特种作业证考试题库及答案

C language simple student management system (including source code)

Ping port artifact psping

National vocational college skills competition (secondary vocational group) network security competition questions - Analysis

2022 Guangdong provincial competition - code information acquisition and analysis flag

Programming example of stm32f1 and stm32subeide -74hc595 drives 4-bit 7-segment nixie tube

【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
随机推荐
[matlab] matlab simulation - simulate the AM modulation process of the modulation system
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
A summary of the 8544 problem that SolidWorks Standard cannot obtain a license
BUU-Crypto-[GUET-CTF2019]BabyRSA
TCP state transition diagram
[paper summary] zero shot semantic segmentation
Unity2D--人物移动并转身
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
Flutter calls Gaode map app to realize location search, route planning and reverse geocoding
[QT] create mycombobox click event
2022 t elevator repair operation certificate examination question bank and simulation examination
[matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
Unity is connected to the weather system
2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
【QT】制作MyComboBox点击事件
Viewing and using binary log of MySQL
June 2022 summary
Yyds dry goods inventory TCP & UDP
PostgreSQL has officially surpassed mysql. Is this guy too strong!
[matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal