尚未完整,待细化

本文总结于:https://developer.mozilla.org/en-US/docs/Glossary/CORS

本文参考了:https://fetch.spec.whatwg.org/#cors-request

CORS时,何时会发CORS-preflight request

下面任意一个条件被满足时,就会发CORS-preflight request。即:

一、如果使用了下面任一个HTTP METHOD,则就发CORS-preflight request

  1. PUT
  2. DELETE
  3. CONNECT
  4. OPTIONS
  5. TRACE
  6. PATCH

二、或者,如果CORS-actual request设置了下面header以外的header,则就发CORS-preflight request

  1. `User-Agent`
  2. `Accept-Charset`
  3. `Accept-Encoding`
  4. `Access-Control-Request-Headers`
  5. `Access-Control-Request-Method`
  6. `Connection`
  7. `Content-Length`
  8. `Cookie`
  9. `Cookie2`
  10. `Date`
  11. `DNT`
  12. `Expect`
  13. `Host`
  14. `Keep-Alive`
  15. `Origin`
  16. `Referer`
  17. `TE`
  18. `Trailer`
  19. `Transfer-Encoding`
  20. `Upgrade`
  21. `Via`
  22. `Proxy-`或以它开头的
  23. `Sec-`或以它开头的
  24. `Accept`
  25. `Accept-Language`
  26. `Content-Language`
  27. Content-Type( with a MIME type of its parsed value (ignoring parameters) of either application/x-www-form-urlencoded, multipart/form-data, or text/plain. )。即:出现Content-Type时,若它的值是 application/x-www-form-urlencoded, multipart/form-data, or text/plain 之一时,不发CORS-preflight request;否则就发。
  28. `DPR`
  29. `Downlink`
  30. `Save-Data`
  31. `Viewport-Width`
  32. `Width`

三、或者,如果Content-Type设置了三个值以外的值时,则就发CORS-preflight request。(注:上面已经说过了,不过,为了和原文结构保持一致,再列一次该规则)

  1. 出现Content-Type时,若它的值是 application/x-www-form-urlencoded, multipart/form-data, or text/plain 之一时,不发CORS-preflight request;否则就发。

四、或者,如果CORS-actual request中的XMLHttpRequestUpload对象注册了1或多个event listeners,则就发CORS-preflight request

五、或者,如果CORS-actual request中用到了ReadableStream对象,则就发CORS-preflight request

再来看下和CORS相关的全部header

CORS相关的全部header

CORS headers
NO. header 举例
  下面两个header:放在 CORS-preflight request 中
1

Access-Control-Request-Method

放在 CORS-preflight request 中。

它告知服务器后续的CORS-actual request将使用这个HTTP METHOD。此时,CORS-preflight request的response则回馈`Access-Control-Allow-Methods`。

Access-Control-Request-Method: POST

意思是:它告知服务器:“后续的CORS-actual request将使用 POST”。CORS-preflight request的response则回馈`Access-Control-Allow-Methods`。

     
2

Access-Control-Request-Headers

放在 CORS-preflight request 中。

它告知服务器后续的CORS-actual request可能会使用这些header。CORS-preflight request的response则回馈`Access-Control-Allow-Headers`。

Access-Control-Request-Headers: X-PINGOTHER, Content-Type

意思是:它告知服务器:“后续的CORS-actual request可能会使用 X-PINGOTHER, Content-Type 这两个header”。CORS-preflight request的response则回馈`Access-Control-Allow-Headers`。

     
  下面四个header:放在 CORS-preflight request的response 中。
1

Access-Control-Allow-Methods

放在 CORS-preflight request的response 中。

当CORS-preflight request中包含`Access-Control-Request-Method`时,CORS-preflight request的response则回馈`Access-Control-Allow-Methods`。

它表明当CORS-actual request 访问资源时,所允许使用的HTTP METHOD。

注意:`Allow` header和CORS协议没有任何关系

Access-Control-Allow-Methods: POST, GET, OPTIONS

意思是:对于给定Origin的CORS-actual request,服务端支持的HTTP METHOD是 POST, GET, OPTIONS。

     
2

`Access-Control-Allow-Headers`

放在 CORS-preflight request的response 中。

当CORS-preflight request中包含`Access-Control-Request-Headers`时,CORS-preflight request的response则回馈` Access-Control-Allow-Headers`。

它表明当CORS-actual request 访问资源时,服务端除了支持simple header外,还支持CORS-preflight request的response的`Access-Control-Allow-Headers`所指定的这些header。

注意:simple header(或称为CORS-safelisted request header)总是可以被CORS-actual request所使用的,它们无需再列在CORS-preflight request的response的`Access-Control-Allow-Headers`的值中。simple header是指下面9个header:

  1. Accept,
  2. Accept-Language,
  3. Content-Language,
  4. Content-Type( with a MIME type of its parsed value (ignoring parameters) of either application/x-www-form-urlencoded, multipart/form-data, or text/plain. )。即:出现Content-Type时,只有当它的值是 application/x-www-form-urlencoded, multipart/form-data, or text/plain 之一时,Content-Type才算是simple header。
  5. DPR
  6. Downlink
  7. Save-Data
  8. Viewport-Width
  9. Width

Access-Control-Allow-Headers: X-Custom-Header, Upgrade-Insecure-Requests

意思是:对于给定Origin的CORS-actual request,服务端除了支持simple header,还另外支持X-Custom-Header, Upgrade-Insecure-Requests这两个header。

     
3

Access-Control-Max-Age

放在 CORS-preflight request的response 中。

它表明 CORS-preflight request的response所返回的`Access-Control-Allow-Methods` 和 `Access-Control-Allow-Headers`的值将被缓存的秒数。

-1是禁止缓存。即:每一个CORS-actual request都要先发一次CORS-preflight request。

Firefox最大值是24hours,即86400秒。Firefox默认值呢?文档没说,先空着它,后续我来补充

Chromium最大值是10minutes,即600秒,Chromium默认值是5秒;

Access-Control-Max-Age: 60

意思是:`Access-Control-Allow-Methods` 和 `Access-Control-Allow-Headers`的值将被缓存1分钟。

     
4

Access-Control-Expose-Headers

放在 CORS-preflight request的response 中。

它表明,除了simple response header,还有这些header也可以被暴露出来供client使用。如果想要client能够访问simple response header之外的header,则,必须将它们列在 CORS-actual request 的response 的`Access-Control-Expose-Headers`中。

simple response header(或称为CORS-safelisted response header)是安全的,当CORS处理response时,它们不会被过滤掉。simple response header是指下面6个header:

  1. Cache-Control
  2. Content-Language
  3. Content-Type
  4. Expires
  5. Last-Modified
  6. Pragma

Access-Control-Expose-Headers: Content-Length, X-Kuma-Revision

意思是:client除了能够访问simple response header外,还可以访问Content-Length, X-Kuma-Revision这两个header。

     
  下面两个header:放在 CORS-actual request的response 中。
1

Access-Controll-Allow-Origin

放在 CORS-actual request 的response 中。

它表明 CORS-actual request的response 能否被 来自特定origin的请求 所共享

`Access-Controll-Allow-Origin`的语法如下:

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: <origin>
Access-Control-Allow-Origin: null

`Access-Controll-Allow-Origin`的特定如下:

  1. 当请求中不包含credentials时,*表明允许任何origin访问所请求的资源;
  2. 当请求中包含credentials时,指定*则会报错;
  3. 只能指定一个origin;

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: https://developer.mozilla.org
Access-Control-Allow-Origin: null

     
2

Access-Control-Allow-Credentials

放在 CORS-actual request 的response 中。

当request的credentials mode为"include"时,是否将response暴露给前端JavaScript代码

Request.credentials为"include"时,只有当Access-Control-Allow-Credentials=true时,浏览器才会将response暴露给前端JavaScript代码

Credentials是指cookies, authorization header或TLS client certificates

当其作为preflight request的响应的一部分时,该header表明实际请求是否可以使用credentials。注意:GET无需preflight,因此,如果请求资源时使用了credentials,如果该header没有和资源一起返回,response就被浏览器忽略了,response就不会返回给web content。

Access-Control-Allow-Credentials和 XMLHttpRequest.withCredentials属性或Request()构函的credentials选项 一起使用。对于使用了credentials的CORS请求,为了让浏览器可以把response暴露给前端JavaScript代码,server端(使用Access-Control-Allow-Credentials)和client端(设置XHR或Fetch或Ajax的credentials mode)都得表明它们将包含credentials

Access-Control-Allow-Credentials: true

注意:唯一的候选值就是true,且,大小写敏感。如果不需要credentials,请完全忽略掉个header,而不是将其设置为false。

     
  下面一个header并不是只和CORS相关。
1

Origin

放在 CORS-preflight request 和 CORS-actual request 中,以及普通POST的request中。

`Origin`是request header,它表明fetch是从哪里来的。Origin语法如下:

Origin: ""
Origin: <scheme> "://" <hostname> [ ":" <port> ]

Origin的特点如下:

  1. 非CORS的普通POST请求中也会包含它。
  2. 它和`Referer`类似,但,`Origin`只包含服务器名字;`Referer`则包含完整的路径信息。
  3. 它的值可以为空。这也是有用处的,例如:当source是data URL时。

Referer: http://foo.example/examples/preflightInvocation.html
Origin: http://foo.example

Origin: ""

Origin: https://developer.mozilla.org

意思是:空或者https://developer.mozilla.org。

Cross-Origin Resource Sharing (CORS)相关推荐

  1. Cross-Origin Resource Sharing[1] 跨域资源共享

    Cross-Origin Resource Sharing [1] 跨域资源共享. 中文名 跨域资源共享 外文名 Cross Origin Resource Sharing CORS是一种允许当前域( ...

  2. ajax跨域请求问题:Access to XMLHttpRequest at……has been blocked by CORS policy: Cross origin requests

    背景:本地代码调试时,加载本地资源正常,jquery的AJAX请求服务端资源报错. Access to XMLHttpRequest at '***** ' from origin 'null' ha ...

  3. CORS(Cross-Origin Resource Sharing)跨域资源共享

    CORS(Cross-Origin Resource Sharing)跨域资源共享 由于浏览器同源策略的影响,跨域问题在我们日常开发中经常遇到,此前有一系列的跨域问题的解决方案,如JSONP.flas ...

  4. 跨域的另一种解决方案CORS(CrossOrigin Resource Sharing)跨域资源共享

    在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...

  5. Access to script at ‘xxx‘ from origin ‘null‘ has been blocked by CORS policy: Cross origin requests

    前言 本地调试js时,遇到本错误 Google Chrome 84.0.4147.135 (正式版本) (64 位) (cohort: Stable Installs Only) 修订版本 c42bd ...

  6. 跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享

    跨域的另一种解决方案--CORS(Cross-Origin Resource Sharing)跨域资源共享 参考文章: (1)跨域的另一种解决方案--CORS(Cross-Origin Resourc ...

  7. 【VSCode】from origin ‘null‘ has been blocked by CORS policy: Cross origin requests are only supported

    Access to script at 'file:///C:/Users/dawulei/Desktop/%E9%A1%B9%E7%9B%AE/%E5%9D%A6%E5%85%8B%E5%A4%A7 ...

  8. CORS(Cross-Origin Resource Sharing) 跨域资源共享

    CORS 全称:Cross-Origin Resource Sharing 中文意思:跨域资源共享? 好吧,目前中文方面的资料还比较少,能搜索到的那仅有的几篇相关介绍,也几乎是雷同的. 最近工作上也有 ...

  9. 解决Cross origin requests are only supported for protocol schemes问题

    解决Cross origin requests are only supported for protocol schemes问题 问题描述 本前端小白在学习的时候尝试按照一个网上的blog的代码来跑 ...

最新文章

  1. CSS3---8.盒模型
  2. Markdown学习测试
  3. LaTeX半小时速成究极进化版(修改版)
  4. 六十七、二分查找算法及其四个变形问题
  5. JavaScript事件处理的三种方式(转)
  6. 从一次换机器的过程谈软硬件的分离
  7. 【BZOJ1084】【codevs2454】最大子矩阵,DP
  8. 8年前,被董明珠痛批卖米粉的北大硕士,如今怎么样了?
  9. 字典添加数据_【Python基础学习】4. 数据类型之字典及其操作
  10. Java 用DBCP连接数据库。
  11. 洛谷——P1319 压缩技术
  12. 近些年很火的Java,就业前景到底怎么样?
  13. 【MIMO通信】基于matlab空时分组编码MIMO_OFDM通信系统仿真【含Matlab源码 663期】
  14. 变异数分析_人工智能系统中分析变异的祸害
  15. linux背光命令,在Linux下开启背光Led键盘的背光
  16. 批量将多个 Word 文档合并成一个 Word 文件
  17. VB连接SQL数据库的方法
  18. python之变量类型
  19. vivado对mcs文件固化
  20. 是什么让你在赛博空间更好看?

热门文章

  1. Tampermonkey(油猴子)安装使用+脚本分享
  2. 如何解决 安装Vmware报msi安装包有问题
  3. linux的su,su - 和sudo的区别
  4. EasyUI datagrid-detailview
  5. 【网络安全】Windows系统安全实验
  6. 想要推翻 5 毛特效,国产影视动画究竟该如何制作?
  7. 冒泡排序法(超详细)
  8. c++ opencv数字图像处理:频率域滤波--低通滤波--理想低通滤波
  9. LaTeX 术语集 Nomenclatures
  10. Lua基础学习--附代码,运行截图