1. 程式人生 > 其它 >laravel 5.6 API 介面開發限制介面訪問頻率

laravel 5.6 API 介面開發限制介面訪問頻率

在laravel 5.6及以上版本中框架中已自帶ThrottleRequests,但是為了更好的處理訊息,我們可以再新加一箇中間件,來更方便的處理相應資訊

第一步: php artisan make:middlewareThrottleRequests

在ThrottleRequest類中放入以下程式碼

namespace App\Http\Middleware;


use Closure;
use Illuminate\Cache\RateLimiter;
use Symfony\Component\HttpFoundation\Response;

class ThrottleRequests
{
    
/** * The rate limiter instance. * * @var \Illuminate\Cache\RateLimiter */ protected $limiter; /** * Create a new request throttler. * * @param \Illuminate\Cache\RateLimiter $limiter */ public function __construct(RateLimiter $limiter) { $
this->limiter = $limiter; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param int $maxAttempts 最大請求次數 * @param int $decayMinutes 時間,分為單位 * @return mixed */ public function handle($request, Closure $next, $maxAttempts = 60
, $decayMinutes = 1) { $key = $this->resolveRequestSignature($request); if ($this->limiter->tooManyAttempts($key, $maxAttempts, $decayMinutes)) { return $this->buildResponse($key, $maxAttempts); } $this->limiter->hit($key, $decayMinutes); $response = $next($request); return $this->addHeaders( $response, $maxAttempts, $this->calculateRemainingAttempts($key, $maxAttempts) ); } /** * Resolve request signature. * * @param \Illuminate\Http\Request $request * @return string */ protected function resolveRequestSignature($request) { return $request->fingerprint(); } /** * Create a 'too many attempts' response. * * @param string $key * @param int $maxAttempts * @return \Illuminate\Http\Response */ protected function buildResponse($key, $maxAttempts) { $message = json_encode([ 'data' => [ 'messsage' => '請求次數太多' //may comes from lang file ,'code' => 429, //your custom code 'data'=>'' ] ]); $response = new Response($message, 429); $retryAfter = $this->limiter->availableIn($key); return $this->addHeaders( $response, $maxAttempts, $this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter), $retryAfter ); } /** * Add the limit header information to the given response. * * @param \Symfony\Component\HttpFoundation\Response $response * @param int $maxAttempts * @param int $remainingAttempts * @param int|null $retryAfter * @return \Illuminate\Http\Response */ protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null) { $headers = [ 'X-RateLimit-Limit' => $maxAttempts, 'X-RateLimit-Remaining' => $remainingAttempts, ]; if (!is_null($retryAfter)) { $headers['Retry-After'] = $retryAfter; $headers['Content-Type'] = 'application/json'; } $response->headers->add($headers); return $response; } /** * Calculate the number of remaining attempts. * * @param string $key * @param int $maxAttempts * @param int|null $retryAfter * @return int */ protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null) { if (!is_null($retryAfter)) { return 0; } return $this->limiter->retriesLeft($key, $maxAttempts); } }

使用:

使用,可以在api.php 路由中這樣使用

$api = app('Dingo\Api\Routing\Router');
 
 
 
$api->version('v1', function ($api) {
 
$api->group(['middleware'=>'throttle:3,1'] , function ($api) {
 
其中3表示請求次數,1為分鐘。以上設定為每分鐘只能請求3 次
或者在Kernel.php $middlewareGroups中配置

 
  'api' => [
            'throttle:3,1',
            'bindings',
        ],