Skip to content

Commit 3fb6192

Browse files
authored
fix: 跨域失效 (#394)
1 parent 69e39f5 commit 3fb6192

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

apps/common/middleware/cross_domain_middleware.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,23 @@ class CrossDomainMiddleware(MiddlewareMixin):
1717

1818
def process_request(self, request):
1919
if request.method == 'OPTIONS':
20-
auth = request.META.get('HTTP_AUTHORIZATION')
21-
if auth is not None and str(auth).startswith("application-"):
22-
application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=auth).first()
23-
if application_api_key.allow_cross_domain:
24-
return HttpResponse(status=200,
25-
headers={
26-
"Access-Control-Allow-Origin": "*" if application_api_key.cross_domain_list is None or len(
27-
application_api_key.cross_domain_list) == 0 else ",".join(
28-
application_api_key.cross_domain_list),
29-
"Access-Control-Allow-Methods": "GET,POST,DELETE,PUT",
30-
"Access-Control-Allow-Headers": "Origin,X-Requested-With,Content-Type,Accept,Authorization,token"})
20+
return HttpResponse(status=200,
21+
headers={
22+
"Access-Control-Allow-Origin": "*",
23+
"Access-Control-Allow-Methods": "GET,POST,DELETE,PUT",
24+
"Access-Control-Allow-Headers": "Origin,X-Requested-With,Content-Type,Accept,Authorization,token"})
3125

3226
def process_response(self, request, response):
3327
auth = request.META.get('HTTP_AUTHORIZATION')
34-
if auth is not None and str(auth).startswith("application-"):
28+
origin = request.META.get('HTTP_ORIGIN')
29+
if auth is not None and str(auth).startswith("application-") and origin is not None:
3530
application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=auth).first()
3631
if application_api_key.allow_cross_domain:
37-
response['Access-Control-Allow-Origin'] = "*" if application_api_key.cross_domain_list is None or len(
38-
application_api_key.cross_domain_list) == 0 else ",".join(
39-
application_api_key.cross_domain_list)
4032
response['Access-Control-Allow-Methods'] = 'GET,POST,DELETE,PUT'
4133
response[
4234
'Access-Control-Allow-Headers'] = "Origin,X-Requested-With,Content-Type,Accept,Authorization,token"
35+
if application_api_key.cross_domain_list is None or len(application_api_key.cross_domain_list) == 0:
36+
response['Access-Control-Allow-Origin'] = "*"
37+
elif application_api_key.cross_domain_list.__contains__(origin):
38+
response['Access-Control-Allow-Origin'] = origin
4339
return response

0 commit comments

Comments
 (0)