php 无法重置限制

cotxawn7  于 2023-01-04  发布在  PHP
关注(0)|答案(2)|浏览(106)

我在我的站点上使用laravel 8,我使用Illuminate\Support\Facades\RateLimiter来定义我的自定义节流阀示例如下

RateLimiter::for('stop', function (Request $request) {
            $ip = $request->ip();
            $maxAttempts = 5;
            
            $limit = Limit::perMinutes((60 * 24), $maxAttempts)->by('stop_'.$ip);
            return $limit;
        });

我想清除这个限制,所以我使用

app(\Illuminate\Cache\RateLimiter::class)->clear('stop_'.$request->ip());

还有这个代码

app(\Illuminate\Cache\RateLimiter::class)->resetAttempts('stop_'.$request->ip());

但这些都不会工作后,重新加载页面仍然显示429错误我想清除限制在某些条件下,但我似乎不能使这项工作如何才能清除限制,而不等待计时器?我想将其设置为24小时,但应该能够允许用户重置它时,正确验证.

8yparm6h

8yparm6h1#

我对代码进行了深入研究,发现密钥在存储该高速缓存之前进行了散列,但当直接发送clear方法时,散列并不包含在其中,因此clear应该是:

app(\Illuminate\Cache\RateLimiter::class)->clear(md5(<limiterName><key>));

对于我的代码应该是

app(\Illuminate\Cache\RateLimiter::class)->clear(md5('stopstop_'.$request->ip()));
eivgtgni

eivgtgni2#

app(\Illuminate\Cache\RateLimiter::class)->clear(md5('stopstop_'.$request->ip()));

use Illuminate\Support\Facades\RateLimiter;

RateLimiter::class->clear(md5('stopstop_'.$request->ip()));

相关问题