自定义跨域中间件

from django.utils.deprecation import MiddlewareMixin


class CorsMiddleware(MiddlewareMixin):
   """屏蔽CORS跨域"""

   def process_response(self, request, response):
       # 使用*表示所有的跨域请求都可以,这样会有风险
       response['Acess-Control-Allow-Origin'] = '*'
       if request.method == 'OPTIONS':
           response['Acess-Control-Allow-Credentials'] = 'true'
           response['Acess-Control-Allow-Header'] = 'content-type, X-Requested-With, X-CSRFToken'
           response['Acess-Control-Allow-Methods'] = 'GET,POST,PUT'
       return response
posted @ 2023-02-15 13:59  vonmo  阅读(16)  评论(0编辑  收藏  举报