swsnu / swppfall2019

31 stars 23 forks source link

[small tip] 알 수 없는 이유로 CSRF확인이 안될때 #187

Closed lightb0x closed 10 months ago

lightb0x commented 4 years ago

오늘 실습시간에 발견하고 조교님 도움 덕분에 해결한 문제입니다. 찾기도 힘들고 다른 팀에서도 발생할 수 있는 문제라고 생각해서 공유합니다.

증상

다른 컴퓨터들에서는 잘 되는 코드가 한 컴퓨터에서만 CSRF에러가 납니다. (403) 가상환경 설정하고 새로 코드를 받아도 똑같이 CSRF 에러가 납니다.

해결

장고 settings.pyMIDDLEWARE에서, 'corsheaders.middleware.CorsMiddleware'를 최대한 위에 두면 해결됩니다. 이는 django-cors-headers에서 권장하고 있는 설정방법입니다.

CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django's CommonMiddleware or Whitenoise's WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.

ktaebum commented 4 years ago

해결이 되었군요 다행입니다 🙂