django针对特定未经身份验证的基于用户的ip地址限制速率



我按照给定的说明进行了设置https://www.django-rest-framework.org/api-guide/throttling/我的设置代码是设置.py

"quot">

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'anon': '5/min',
'anon_day': '10/day',
'user': '10/min',
'user_day': '20/day',
}

"quot">

和自定义节流.py

"quot">

from rest_framework.throttling import AnonRateThrottle, UserRateThrottle

class AnonDayThrottle(AnonRateThrottle):
scope = 'anon_day'

class UserDayThrottle(UserRateThrottle):
scope = 'user_day'

"quot"并分配给api类comparable_summary.py"quot">

from rest_framework.throttling import AnonRateThrottle
from rest_framework.views import APIView
from .throttle import AnonDayThrottle

LOGGER = logging.getLogger(__name__)

class ComparablesSummaryAPI(APIView):
"""
apis performs for given response
"""
throttle_classes = [AnonDayThrottle, AnonRateThrottle]

"quot">

我的问题是,我已经检查了两个不同的ip地址,但它认为所有请求都是唯一的ip地址。请帮助我感谢的帮助

应用程序实例前面有代理吗?如果是,您可能需要设置django-rest框架的NUM_PROXIES设置。

如果您需要严格标识唯一的客户端IP地址,则需要首先通过设置NUM_proxies设置来配置API运行的应用程序代理的数量。此设置应为零或更大的整数。如果设置为非零,则在首次排除任何应用程序代理IP地址后,客户端IP将被标识为X-Forwarded-For标头中的最后一个IP地址。如果设置为零,则REMOTE_ADDR值将始终用作识别IP地址。

  • https://www.django-rest-framework.org/api-guide/throttling/#how-已识别客户端
  • https://oxpedia.org/wiki/index.php?title=AppSuite:Grizzly#Multiple_Proxies_in_front_of_the_cluster

最新更新