设置未知属性:yii\filters\RateLimiter::rateLimit Yii2 速率限制 API

Setting unknown property: yii\filters\RateLimiter::rateLimit Yii2 Rate Limiting Api

提问人:Gnoul 提问时间:11/9/2023 最后编辑:Brian Tompsett - 汤莱恩Gnoul 更新时间:11/10/2023 访问量:39

问:

我正在尝试在 yii2 中实现 rateLimit 模块来保护我的 API,因此我首先实现并添加了文档中的 , , 函数。RateLimitInterfacegetRateLimitloadAllowancesaveAllowance

use yii\filters\RateLimitInterface;
class User extends \yii\db\ActiveRecord implements IdentityInterface, RateLimitInterface
{ 
    public $rateLimit = 1;
    public $allowance;
    public $allowance_updated_at; 
...
    public function getRateLimit($request, $action)
    {
        return [$this->rateLimit, 60];
    }

    public function loadAllowance($request, $action)
    {
        return [$this->allowance, $this->allowance_updated_at];
    }

    public function saveAllowance($request, $action, $allowance, $timestamp)
    {
        $this->allowance = $allowance;
        $this->allowance_updated_at = $timestamp;
        $this->save();
    }
}

然后在我的UserController中,我有这个:

<?php

namespace app\controllers;

use yii\rest\ActiveController;
use yii\web\Response;
use yii\filters\auth\HttpBasicAuth;
use app\models\User;
use app\controllers\Yii;
use yii\base\Security;
use yii\helpers\ArrayHelper;
use yii\filters\RateLimiter;
use app\components\MyRateLimiter;

class UserController extends ActiveController
{
    

    public $modelClass = 'app\models\User';

    public function actions()
    {
        return ArrayHelper::merge(parent::actions(), [
            'index' => [
                'pagination' => [
                    'pageSize' => 0,
                ],
            ],
        ]);
    }

    public function behaviors()
    {

        $behaviors = parent::behaviors();

        $behaviors['rateLimiter'] = [
            'class' => \yii\filters\RateLimiter::class,
            'enableRateLimitHeaders' => true, // Enable rate limit headers
        ];

        $behaviors['contentNegotiator'] = [

            'class' => 'yii\filters\ContentNegotiator',

            'formats' => [

                'application/json' => Response::FORMAT_JSON,

            ]

        ];


        $behaviors['authenticator'] = [
            'class' => HttpBasicAuth::className(),
            'auth' => function ($username, $password) {
                \Yii::info("Le système tente de se connecter avec un nom d'utilisateur et un token d'accès", 'auth');
                $user = User::find()->where(['username' => $username, 'acces_api_user' => 1])->one();
                if ($user !== null && $user->validatePassword($password)) {
                    return $user;
                }
                return null;
            },
        ];
        

        return $behaviors;

    }

    


}

我正在尝试检查我的 API 连接的次数,然后防止暴力破解; 我得到的错误如下:

设置未知属性:yii\filters\RateLimiter::rateLimit

php yii2-user

评论


答: 暂无答案