簡體   English   中英

Django-ratelimit 不限制

[英]Django-ratelimit does not limit

我在 heroku 上使用 Django-ratelimit 時遇到限制器不起作用的問題。 我沒有收到任何錯誤。 任何建議我做錯了什么?

視圖.py

from django.core.cache import cache
from ratelimit.mixins import RatelimitMixin

[...]

class LoginView(RatelimitMixin, FormView):
    ratelimit_key = 'user'
    ratelimit_rate = '1/5m'
    ratelimit_method = 'GET'
    ratelimit_block = True

    template_name = "account/login.html"
    template_name_ajax = "account/ajax/login.html"
    form_class = LoginUsernameForm
    form_kwargs = {}
    redirect_field_name = "next"

    @method_decorator(sensitive_post_parameters())
    @method_decorator(csrf_protect)
    @method_decorator(never_cache)
    def dispatch(self, *args, **kwargs):
        return super(LoginView, self).dispatch(*args, **kwargs)

    def get(self, *args, **kwargs):
        if is_authenticated(self.request.user):
            return redirect(self.get_success_url())
        return super(LoginView, self).get(*args, **kwargs)

設置.py

# RATELIMIT SETTINGS
#RATELIMIT_CACHE_PREFIX = 'rl:'
RATELIMIT_ENABLE = True
RATELIMIT_USE_CACHE = 'default'
#RATELIMIT_VIEW = None

只是對可能出錯的一些想法。 請注意,我從未使用過這個應用程序,我只是查看了ratelimit 的文檔

ratelimit_key更改為ip ,而不是user

由於它在登錄頁面上,我相信user密鑰不會有任何影響,因為它依賴於request.user

可能你想要的是改用ip

class LoginView(RatelimitMixin, FormView):
    ratelimit_key = 'ip'
    ratelimit_method = 'POST'

它可能需要您將ratelimit_method更改為POST 至少對我來說更有意義。

閱讀有關Ratelimit Keys - Common Keys 的更多信息

PS:既然你提到你已經在 Heroku 上部署了你的應用程序,那么獲取客戶端的 IP 地址可能會出現問題,這可能是 django-ratelimt 應用程序使用的。 閱讀有關此 SO 問題的更多信息: Get client's real IP address on Heroku

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM