Implementing an Admin Backend IP Whitelist with Django Middleware
Preface Django's built-in Admin management backend is very convenient, but there really are quite a few security issues in actual production environments. Regarding admin security protection, I previously researched and implemented adding a login captcha and rate limiting to admin. You can refer to this article: Adding Captcha and Multiple Login Attempt Limits to Django Admin However, it's much better if it's only used internally, so we can implement IP filtering through Django middleware. Therefore this article involves two knowledge points: one is Django middleware, and the other is P
目录5 个章节
Preface
Django's built-in Admin management backend is very convenient, but there really are quite a few security issues in actual production environments. Regarding admin security protection, I previously researched and implemented adding a login captcha and rate limiting to admin. You can refer to this article: Adding Captcha and Multiple Login Attempt Limits to Django Admin
However, it's much better if it's only used internally, so we can implement IP filtering through Django middleware. Therefore this article involves two knowledge points: one is Django middleware, and the other is how Python handles IP as a data structure.
Let's get into the main topic~
Django Middleware
The concept and usage of middleware are not the focus of this article; please refer to the official documentation yourself—it's written very clearly and easy to understand.
First, we create a new Python package: middleware, and create a Python file admin_secure.py to write our middleware:
import ipaddress
from django.conf import settings
from django.http.response import HttpResponseForbidden
class AdminSecureMiddleware(object):
"""
Admin 安全中间件
用于限制可访问 admin 管理后台的IP段
"""
admin_url = f'/{settings.URL_PREFIX}admin'
allow_networks = [
ipaddress.ip_network('10.53.0.0/20'),
]
allow_addresses = []
@classmethod
def get_allow_addresses(cls):
if len(cls.allow_addresses) > 0:
return cls.allow_addresses
for network in cls.allow_networks:
for ip in network:
cls.allow_addresses.append(ip)
return cls.allow_addresses
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
ip_addr = ipaddress.ip_address(request.META.get('REMOTE_ADDR'))
if request.path.startswith(self.admin_url):
if ip_addr not in self.get_allow_addresses():
return HttpResponseForbidden()
return response
Note the code above: you can add the allowed IP network segments in allow_networks , then enable this middleware in settings.py and you're good to go.
This then brings up the second knowledge point covered in this article, which is Python's ipaddress operations.
ipaddress Module
I suddenly feel like slacking off, hahahahaha~
Because this part is really quite simple. The official documentation is: https://docs.python.org/zh-cn/3/howto/ipaddress.html
You haven't given your college computer networking knowledge back to the teacher yet, right (flees~)
References
Welcome to Discuss
I've compiled a series of technical articles and materials. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~
_EOF_
Implementing an Admin Backend IP Whitelist with Django Middleware
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
轻松搞定Django分页功能
view代码 from django.core.paginator import Paginator def announcement(request): ctx = { 'global': GlobalCtx, 'announcements': models.Announcement.objects.all(), } paginator
2022-05-27 · Django · 约 1 分钟
给Django的Admin添加自定义Action-并移除需要选择对象的限制
前言 不得不说,Django的Admin真的给开发带来很多便利,这不,我又来折腾了,这次是添加自定义的action 这个自定义的Action可以看看官方文档的介绍,很详细,不再赘述。 https://docs.djangoproject.com/en/3.0/ref/contrib/admin/actions/ 界面效果 Django默认的添加完action是要选择下面的对象才能提交的,不过我不需
2022-08-26 · Django · 约 1 分钟
Django的信号机制入门
概念 django自带一套信号机制来帮助我们在框架的不同位置之间传递信息。也就是说,当某一事件发生时,信号系统可以允许一个或多个发送者(senders)将通知或信号(signals)发送给一组接受者(receivers)。 (感觉就很像Qt的信号与槽机制) 信号系统包含以下三要素: 发送者-信号的发出方 信号-信号本身 接收者-信号的接受者 Django内置了一整套信号,下面是一些比较常用的: 在
2022-08-26 · Django · 约 1 分钟
分享一些2026年有意思的现代化Django生态组件
2026年了,若还在忍受石器时代的模板和冗余CRUD,趁早转行。Cotton和Inertia总算给Django这老古董续了命,Ninja这类工具已是开发及格线。看一眼这些现代化组件吧,别再用那些自我感动的低效代码来折磨审美了。
2026-01-21 · Django · 约 1 分钟
讨论
共 0 条加载评论中…