html部分:
<script type="text/javascript" src="../static/js/jquery-1.11.3.js"></script>
<script> $(document).ready(function(){ $("button").click(function(){ $.ajax({ url: 'http://127.0.0.1:8000/', type:'get', dataType: 'json', beforeSend: function(xhr) { xhr.setRequestHeader('Authorization','TOKEN ' + 'x048b18739ca6c46f8365c258f5'); }, success: function(data, status) { return console.log(data); } }); }); }); </script> django 安装
django-cors-headers
settings.py 配置
Install by downloading the source and running:
python setup.py install
or
pip install django-cors-headers
and then add it to your installed apps:
INSTALLED_APPS = (
...
'corsheaders',
...
)
You will also need to add a middleware class to listen in on responses:
MIDDLEWARE_CLASSES = (
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
)
Note that CorsMiddleware
needs to come before Django's CommonMiddleware
if you are using Django's USE_ETAGS = True
setting, otherwise the CORS headers will be lost from the 304 not-modified responses, causing errors in some browsers.
Configuration
Add hosts that are allowed to do cross-site requests to CORS_ORIGIN_WHITELIST
or set CORS_ORIGIN_ALLOW_ALL
to True
to allow all hosts.
CORS_ORIGIN_ALLOW_ALL: if True, the whitelist will not be used and all origins will be accepted
Default:
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST: specify a list of origin hostnames that are authorized to make a cross-site HTTP request
Example:
CORS_ORIGIN_WHITELIST = (
'google.com',
'hostname.example.com'
)
Default:
CORS_ORIGIN_WHITELIST = ()
CORS_ORIGIN_REGEX_WHITELIST: specify a regex list of origin hostnames that are authorized to make a cross-site HTTP request; Useful when you have a large amount of subdomains for instance.
Example:
CORS_ORIGIN_REGEX_WHITELIST = ('^(https?://)?(\w+\.)?google\.com$', )
Default:
CORS_ORIGIN_REGEX_WHITELIST = ()
You may optionally specify these options in settings.py to override the defaults. Defaults are shown below:
CORS_URLS_REGEX: specify a URL regex for which to enable the sending of CORS headers; Useful when you only want to enable CORS for specific URLs, e. g. for a REST API under
/api/
.
Example:
CORS_URLS_REGEX = r'^/api/.*$'
Default:
CORS_URLS_REGEX = '^.*$'
CORS_ALLOW_METHODS: specify the allowed HTTP methods that can be used when making the actual request
Default:
CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
CORS_ALLOW_HEADERS: specify which non-standard HTTP headers can be used when making the actual request
Default:
CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
CORS_EXPOSE_HEADERS: specify which HTTP headers are to be exposed to the browser
Default:
CORS_EXPOSE_HEADERS = ()
CORS_PREFLIGHT_MAX_AGE: specify the number of seconds a client/browser can cache the preflight response
Note: A preflight request is an extra request that is made when making a "not-so-simple" request (eg. content-type is not application/x-www-form-urlencoded) to determine what requests the server actually accepts. Read more about it here: [http://www.html5rocks.com/en/tutorials/cors/](http://www.html5rocks.com/en/tutorials/cors/)
Default:
CORS_PREFLIGHT_MAX_AGE = 86400
CORS_ALLOW_CREDENTIALS: specify whether or not cookies are allowed to be included in cross-site HTTP requests (CORS).
Default:
CORS_ALLOW_CREDENTIALS = False
CORS_REPLACE_HTTPS_REFERER: specify whether to replace the HTTP_REFERER header if CORS checks pass so that CSRF django middleware checks will work with https
Note: With this feature enabled, you also need to add the corsheaders.middleware.CorsPostCsrfMiddleware after django.middleware.csrf.CsrfViewMiddleware to undo the header replacement
Default:
CORS_REPLACE_HTTPS_REFERER = False
相关推荐
用过Django 进行开发的同学都知道,Django框架天然支持对CSRF攻击的防护,因为其内置了一个名为CsrfViewMiddleware的中间件,其基于Cookie方式的防护原理,相比基于session的方式,更适合目前前后端分离的业务场景。...
通过理解Django的CSRF防护机制,开发者可以更好地保护其Web应用免受跨站请求伪造的攻击,确保用户数据的安全。在开发过程中,始终遵循最佳安全实践,定期审计和测试应用的防护措施,是保障Web应用安全的重要环节。
在Django框架中,CSRF(Cross-Site Request Forgery,跨站请求伪造)是一种重要的安全防护机制,用于防止恶意第三方在用户浏览器中利用已登录用户的权限执行操作。当你遇到"CSRF token missing or incorrect"的错误...
总的来说,Django的CSRF保护机制是为了防止恶意的跨站请求,确保用户只能执行他们自己发起的操作。在使用POST请求时,无论是通过Ajax还是传统的表单提交,都需要确保正确地处理和传递CSRF令牌,以维护应用的安全性。...
npm install --save django-react-csrftoken 用法 import React from 'react' ; import DjangoCSRFToken from 'django-react-csrftoken' class MyLoginForm extends React . Component { render ( ) { return ( ...
这个问题是由于Django的安全机制——跨站请求伪造(Cross-Site Request Forgery,简称CSRF)防护导致的。本文将详细介绍如何解决这个常见的Django Ajax问题。 **1. CSRF保护机制** Django内置了CSRF保护,目的是...
补丁加csrftoken从饼干作为X-CSRFToken头球每个SuperAgent的要求。 用法 // Just require patch require ( 'superagent-django-csrf' ) ; 安装 npm install superagent-django-csrf 执照 麻省理工学院:copyright:
在前后端分离的Web开发模式下,Django的CSRF(Cross Site Request Forgery,跨站请求伪造)保护机制可能会引发问题,因为它主要是为传统的基于表单的提交设计的。然而,现代应用往往使用Ajax进行异步通信,这就需要...
CSRF,全称为Cross-Site Request Forgery(跨站请求伪造),是一种安全威胁,通常与另一种常见攻击手法XSS(Cross-Site Scripting,跨站脚本攻击)一起被提及。尽管两者都涉及跨站攻击,但它们的工作原理和攻击方式...
Django框架在处理跨域请求(Cross-Origin Resource Sharing, CORS)时,面临CSRF(Cross-Site Request Forgery,跨站请求伪造)的安全风险。本文将探讨Django如何安全地实现跨域请求同时防范CSRF攻击,并提供相关的...
Django框架内置了CSRF保护机制,该机制可以有效防止跨站请求伪造攻击。在Django中,CSRF保护机制默认是开启的。Django通过在每个表单中添加一个不可见的输入字段(隐藏字段)来确保数据的来源是合法的。这个隐藏字段...
Django CSRF(跨站请求伪造)是Web应用中一种安全机制,用于防止恶意第三方伪造用户的请求。在Django框架中,CSRF保护是默认启用的,以确保只有合法的用户操作才能被执行。以下是对标题、描述和标签内容的详细解释:...
今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败;先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了CSRF的问题了;很显然,用ajax发送post请求这样就白搭了; 文末...
CSRF(Cross-Site Request Forgery,跨站请求伪造),是一种常见的安全攻击手段。在这种攻击中,攻击者利用受害者的身份(通常是通过受害者已登录状态下的Cookies)发起恶意请求。这些请求对于服务器而言是合法的,...
在Django框架中,CSRF(Cross-site request forgery,跨站请求伪造)是一种重要的安全机制,用于防止恶意第三方在用户浏览器中伪造请求到你的应用。然而,在某些情况下,例如前后端分离的项目或者API接口,你可能...
CSRF 保护在每个带有X-CSRFToken标头的 AJAX 请求上默认启用。 您可以在config/environment.js指定 URL 模式以仅保护一部分请求。 var ENV = { ... django : { csrf : '^api' } } ; 执照 Ember Django CSRF ...
总的来说,Django的CSRF认证实现是其安全性的重要组成部分,有效地防止了恶意的跨站请求,保护了用户的数据安全。通过理解其工作原理和中间件执行流程,开发者可以更好地实施和优化CSRF防护策略。
**四、处理Django CSRF(跨站请求伪造)** Django默认对POST请求启用CSRF保护。对于Ajax POST请求,需要在前端发送请求时包含CSRF令牌。可以使用jQuery的`$.ajaxSetup()`全局配置: ```javascript $.ajaxSetup({ ...