当前位置:网站首页>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__',
]);边栏推荐
- SQL injection - injection based on MSSQL (SQL Server)
- Remote desktop client RDP
- A summary of the 8544 problem that SolidWorks Standard cannot obtain a license
- Ping port artifact psping
- Topological sorting and graphical display of critical path
- 十二. golang其他
- Daily question brushing record (12)
- TCP state transition diagram
- Character types of C language
- PostgreSQL has officially surpassed mysql. Is this guy too strong!
猜你喜欢

The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat

(4) Canal multi instance use

Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology

Flutter ‘/usr/lib/libswiftCore. dylib‘ (no such file)

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

2022g2 power station boiler stoker special operation certificate examination question bank and answers

【QT】定时器

Enterprise level log analysis system elk (if things backfire, there must be other arrangements)

19.Frambuffer应用编程
![[QT] timer](/img/df/5db6af851ef19f33fd7e7a7ed46586.png)
[QT] timer
随机推荐
ansys命令
[matlab] matlab simulation - narrow band Gaussian white noise
光模块字母含义及参数简称大全
BUU-Crypto-[GXYCTF2019]CheckIn
十二. golang其他
C语言简易学生管理系统(含源码)
c语言经典指针和数组笔试题解析
Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
[matlab] matlab simulation modulation system - DSB system
Trie数-字典树
[matlab] matlab simulation - low pass Gaussian white noise
Talk about the SQL server version of DTM sub transaction barrier function
2022 Guangdong provincial competition - code information acquisition and analysis flag
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
What are the reasons for the frequent high CPU of ECS?
VB. Net simple processing pictures, black and white (class library - 7)
left_and_right_net可解释性设计
[matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal
Supplement the JS of a video website to decrypt the video
LM small programmable controller software (based on CoDeSys) note XXI: error 3703