简体   繁体   中英

Django Allowed Hosts

I want only my front.domain.com to access the django API so i updated my settings.py . When i deployed i can access the django API via curl and postman so i'm confused here is there anything i'm missing !

settings.py

DEBUG = False  # deployment

if DEBUG:
    FRONT_OFFICE_URL = 'http://127.0.0.1:4200/'
    ALLOWED_HOSTS = ['*']  # development
    CORS_ORIGIN_ALLOW_ALL = True # development
else:
    FRONT_OFFICE_URL = 'https://front.domaine.com'
    ALLOWED_HOSTS = [FRONT_OFFICE_URL ]  # deployment
    CORS_ORIGIN_WHITELIST = [FRONT_OFFICE_URL]
    CSRF_TRUSTED_ORIGINS = [FRONT_OFFICE_URL ]



INSTALLED_APPS = [
     ...
    'corsheaders',
   ]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
     ...
   ]

CORS prevents browsers from accessing resources on other domains. But any other http request will not be blocked by CORS. If you need to block requests where Referer is not your domain, you can write some middleware to do that, but beware that it can easily be faked - Postman and Curl both lets you set the Referer header to any value.

If you need to secure requests to the API in Django, setting up CSRF protection is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM