본문 바로가기
Python/Django

Django 로그인 시 CSRF 에러(CSRF verification failed. Request aborted)

by HoneyIT 2022. 7. 12.

Lightsail을 이용한 aws 서버에서 docker 실행 시 위와 같은 에러가 발생했습니다.

CSRF와 관련된 에러로 보여집니다.

 

Help 메세지를 확인해보니 CsrfViewMiddleware를 사용하지 않으면 모든 views에 csrf_protect를 사용해야 한다고 나와있습니다.

 

 

해결 방법은 간단했습니다.

https가 적용되면 csrf를 신뢰할 수 있도록 url을 추가해야 합니다.

Django 버전 4.0이상은 scheme(https://)와 hostname

 

 

버전 3.2이하는 hostname만 추가하면 됩니다.

 

 

docker를 다시 빌드 실행하고 migrate한 후 접속하면 잘 되는 것을 확인할 수 있습니다.

 

 

https://stackoverflow.com/questions/38841109/csrf-validation-does-not-work-on-django-using-https

 

CSRF validation does not work on Django using HTTPS

I am developing an application which the frontend is an AngularJS API that makes requests to the backend API developed in Django Rest Framework. The frontend is on the domain: https://front.bluem...

stackoverflow.com