调试代码

main
RogerWork 8 months ago
parent 1e9a25332a
commit 10cc34903f
  1. 16
      dingxin_toolbox_drf/http.py
  2. 1
      dingxin_toolbox_drf/settings.py

@ -0,0 +1,16 @@
from django.utils.deprecation import MiddlewareMixin
class SetRemoteAddrFromForwardedFor(MiddlewareMixin):
"""
如果部署了代理使用此中间件获取远程客户端IP
在settings中注册该中间件
"""
def process_request(self, request):
try:
real_ip = request.META['HTTP_X_FORWARDED_FOR']
except KeyError as err:
print(err)
else:
real_ip = real_ip.split(",")[0]

@ -91,6 +91,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'dingxin_toolbox_drf.http.SetRemoteAddrFromForwardedFor',
]
ROOT_URLCONF = 'dingxin_toolbox_drf.urls'

Loading…
Cancel
Save