简体   繁体   English

Django REST api调用和allowed_hosts

[英]Django REST api calls and allowed_hosts

I'm trying to implement a RESTful API in Django such that any ip could query the endpoints. 我正在尝试在Django中实现RESTful API,以便任何IP都可以查询端点。 However, I'm concerned about header attacks if I were to set ALLOWED_HOSTS = ['*'] . 但是,如果要设置ALLOWED_HOSTS = ['*'] ,我会担心头攻击。

I read an answer to Why is Django throwing error "DisallowedHost at /"? 我读了一个答案, 为什么Django会抛出错误“ /的DisallowedHost”? which suggests that api calls should be responded to, not for by the server. 这表明api调用应该响应,而不是服务器响应。

I don't full comprehend what they mean or how to implement it and am looking for suggestions. 我不完全理解它们的含义或实现方式,而是在寻找建议。

Ultimately, I want to know how can I make an api call which is not blocked by django because it is not in ALLOWED_HOSTS? 最终,我想知道如何进行django阻止的api调用,因为它不在ALLOWED_HOSTS中?

ALLOWED_HOSTS has nothing to do with your API calls in any way. ALLOWED_HOSTS与您的API调用没有任何关系。 It's the list of hostnames your server should respond for, not to 这是服务器应响应的主机名列表,而不是响应

The problem you are having is not anything to do with ALLOWED_HOSTS, and everything to do with CSRF protection. 您遇到的问题与ALLOWED_HOSTS无关,与CSRF保护无关。 You have two options. 您有两个选择。 You can disable cross site request forgery protection on the page by using either 您可以使用以下任一方法在页面上禁用跨站点请求伪造保护

@method_decorator(csrf_exempt, name=dispatch)

above your class in django >= 1.9, or decorating the dispatch method in previous versions of django, such as this: 在django> = 1.9中位于类的上方,或在django的早期版本中装饰分派方法,例如:

class myView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(myView, self).dispatch(request, *args, **kwargs)

If you are concerned about who can gain access though, you will need to look into other authentication methods, such as token based authentication, so that only sites passing the proper token can get access. 但是,如果您担心谁可以访问,则需要研究其他身份验证方法,例如基于令牌的身份验证,以便只有传递正确令牌的站点才能获得访问权限。

ALLOWED_HOSTS are list of strings or regular expressions representing the host/domain names that this Django site can serve. ALLOWED_HOSTS是表示此Django站点可以服务的主机/域名的stringsregular expressions列表。 You need to use Token, JWT or any other Authentication methods for preventing you APIs. 您需要使用令牌,JWT或任何其他身份验证方法来防止使用API​​。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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