gpt4 book ai didi

django-rest-framework - 身份验证失败过多后如何锁定 IP 地址?

转载 作者:行者123 更新时间:2023-12-04 00:15:37 25 4
gpt4 key购买 nike

在太多身份验证失败后,是否有锁定 IP 地址的常用方法?我不知道内置的节流将如何实现这一点,因为节流仅在身份验证和权限成功后才会启动。

最佳答案

谢谢汤姆。我使用以下代码对身份验证进行了子类化:

def authenticate(self, request):

#
# first check to see that IP address is not locked out
# due to too many failed authentication requests.
#
auth_failure_key = 'LOGIN_FAILURES_AT_%s' % request.META.get('REMOTE_ADDR')

auth_failures = cache.get(auth_failure_key) or 0

# allow up to 3 failures per hour
if auth_failures >= 3:
raise exceptions.AuthenticationFailed('Locked out: too many authentication failures')

try:
return super(TokenAuthentication, self).authenticate(request)
except exceptions.AuthenticationFailed as e:

# update cache
cache.set(auth_failure_key, auth_failures + 1, 3600)

raise e

关于django-rest-framework - 身份验证失败过多后如何锁定 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16216273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com