我希望能够只允许从具有静态IP的公司办公室使用某些应用程序。有没有django包可以用来做这件事?如果是这样,您是否知道它是否可以用作额外的安全措施?我不希望办公室以外的任何人访问某些应用程序和这些应用程序中的数据。我已经有了相当好的应用程序安全性,但作为额外的安全措施,这是一个好的选择吗?谢谢
发布于 2018-08-08 06:22:58
看起来这就是我要找的..。django-iprestrict.readthedocs.io/en/latest/configuration.html软件包将允许您默认允许或拒绝,并设置每个应用的IP白名单或黑名单-
发布于 2018-08-04 11:19:35
只需在settings.py文件中的'MIDDLEWARE_CLASSES‘变量下包含此中间件类即可。
还包括变量BLOCKED_IPS = ('123.123.123.123',)变量,其中的值是您希望阻止站点访问的IP地址的元组。
"""
simple middlware to block IP addresses via settings
variable BLOCKED_IPS
"""
from django.conf import settings
from django import http
class BlockedIpMiddleware(object):
def process_request(self, request):
if request.META['REMOTE_ADDR'] in settings.BLOCKED_IPS:
return http.HttpResponseForbidden('<h1>Forbidden</h1>')
return None
发布于 2021-05-23 05:02:17
这部分基于Ramesh K的回答,并对Django 2.2.19进行了更改。在我使用的服务器上:负载均衡器将接收到的IP地址放入"X-Real- IP“头中(并将"X-Forwarded-For”头作为逗号分隔的IP地址列表传递)。则"REMOTE_ADDR“包含负载均衡器地址,而不是实际的远程地址。
from django.conf import settings
from django import http
class BlockedIpMiddleware:
def __init__(self, get_response):
# One-time configuration and initialization, when the webserver starts.
self.get_response = get_response
def __call__(self, request):
# Code to be executed for each request before the view (and later
# middleware) are called.
# if request.META['REMOTE_ADDR'] in settings.BLOCKED_IPS:
if request.META['HTTP_X_REAL_IP'] in settings.BLOCKED_IPS:
return http.HttpResponseForbidden('<h1>Forbidden</h1>')
return self.get_response(request)
https://stackoverflow.com/questions/51682079
复制相似问题