From 224fe26f2844ee2af92c66881efcc8870e63b83b Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Sat, 12 Jul 2025 11:41:50 +0800 Subject: [PATCH] feat: add DocHeadersMiddleware to handle document access and redirect unauthorized users --- .../middleware/doc_headers_middleware.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 apps/common/middleware/doc_headers_middleware.py diff --git a/apps/common/middleware/doc_headers_middleware.py b/apps/common/middleware/doc_headers_middleware.py new file mode 100644 index 000000000..4024e6ddb --- /dev/null +++ b/apps/common/middleware/doc_headers_middleware.py @@ -0,0 +1,62 @@ +# coding=utf-8 +""" + @project: maxkb + @Author:虎 + @file: static_headers_middleware.py + @date:2024/3/13 18:26 + @desc: +""" +from django.http import HttpResponse +from django.utils.deprecation import MiddlewareMixin + +content = """ + + + + + + + Document + + + + + +""" + + +class DocHeadersMiddleware(MiddlewareMixin): + def process_response(self, request, response): + if request.path.startswith('/doc/') or request.path.startswith('/doc/chat/'): + HTTP_REFERER = request.META.get('HTTP_REFERER') + if HTTP_REFERER is None: + return HttpResponse(content) + if HTTP_REFERER == request._current_scheme_host + request.path: + return response + return response