当前位置:网站首页>跨域概述,简单积累
跨域概述,简单积累
2022-06-24 09:46:00 【右眸Remnant】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、同源策略
提供浏览器的一种安全机制;请求的url地址必须和浏览器上的url同域,即:端口,协议,IP全部相同,如果任意一个不满足,则表示不同源;
如:从127.0.0.1:8000 请求 127.0.0.1:8001
当未进行处理的时候,浏览器会作出反应发生报错进行拦截。
二、CORS
CORS(跨域资源共享),由浏览器和服务器双方共同支持解决跨域问题。
CORS请求分为两种:简单请求和非简单请求
- 请求方法为:HEAD, GET,POST其中一种
- HTTP的头信息不超过:Accept, Accept-Language, Content-Language, Content-type
满足上述两种情况,则表示简单请求
简单请求:仅发送一次
非简单请求:包含两次请求,第一次为OPTIONS(预检请求),如果预检通过则发送第二次请求(真正的数据)
解决跨域的方式:
- 简单请求:
服务器设置响应头:Access-Control-Allow-Origin = '域名' 或 ‘*’
- 非简单请求:
在发送预检请求的时候,允许请求方式:Access-Control-Request-Method; 允许请求头:Access-Control-Request-Headers
实现原理:
当浏览器发现跨域请求的时候,如果是简单请求,会在请求头中添加Origin字段,表示来自哪个源;服务器接收到响应,会在响应头中添加Access-Control-Allow-Origin,指定域名或者通配;
三、Django中的跨域解决方案
1. 在返回结果中允许信息
data = {
"dafd": access_token}
response = HttpResponse(json.dumps(data))
# 处理简单请求
response["Access-Control-Allow-Origin"] = "*"
# 预检方式
response["Access-Control-Allow-Methods"] = "POST, GET"
response["Access-Control-Max-Age"] = "1000"
# 预检请求头
response["Access-Control-Allow-Headers"] = "*"
return response
2. 使用django-cors-headers
django-cors-headers时第三方依赖,使用pip下载,修改settings配置文件
pip install django-cors-headers
在setting.py中添加应用:
INSTALLED_APPS = [
...
'corsheaders',
...
]
添加中间件:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
文件末尾添加:
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
边栏推荐
- Yolov6: the fast and accurate target detection framework is open source
- Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
- Queue queue
- Nvisual digital infrastructure operation management software platform
- Cookie encryption 4 RPC method determines cookie encryption
- 线程的六种状态
- SQL Server AVG function rounding
- tf.contrib.layers.batch_norm
- Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
- SQL Server AVG函数取整问题
猜你喜欢

正规方程、、、

Machine learning - principal component analysis (PCA)

How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?

Using pandas to read SQL server data table

415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)

利用pandas读取SQL Sever数据表

1. project environment construction

411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency

Cookie encryption 4 RPC method determines cookie encryption

p5.js实现的炫酷交互式动画js特效
随机推荐
PHP encapsulates a file upload class (supports single file and multiple file uploads)
SQL Server AVG函数取整问题
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
How large and medium-sized enterprises build their own monitoring system
一群骷髅在飞canvas动画js特效
百度网盘下载一直请求中问题解决
牛客-TOP101-BM29
5.菜品管理业务开发
Wechat applet learning to achieve list rendering and conditional rendering
leetCode-1051: 高度检查器
Go language development environment setup +goland configuration under the latest Windows
3. addition, deletion, modification and query of employees
Open Oracle server under Linux to allow remote connection
MySQL data advanced
时尚的弹出模态登录注册窗口
微信小程序學習之 實現列錶渲染和條件渲染.
numpy.logical_or
Error reading CSV (TSV) file
dedecms模板文件讲解以及首页标签替换
leetCode-498: 對角線遍曆