Django rest framework 如何在多次身份验证失败后锁定IP地址?

Django rest framework 如何在多次身份验证失败后锁定IP地址?,django-rest-framework,Django Rest Framework,在多次身份验证失败后,是否有一种锁定IP地址的常用方法?我不知道内置的节流如何实现这一点,因为节流只有在身份验证和权限成功后才会生效。不是现成的,不是。您需要对其中一个身份验证类进行子类化,并在自定义身份验证类中自己实现该行为。谢谢Tom。我使用以下代码将身份验证子类化: def authenticate(self, request): # # first check to see that IP address is not locked out # due to t

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

不是现成的,不是。您需要对其中一个身份验证类进行子类化,并在自定义身份验证类中自己实现该行为。

谢谢Tom。我使用以下代码将身份验证子类化:

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