您好,欢迎访问一九零五行业门户网

浅谈关于预检请求

背景不知道大家有没有发现,有时候我们在调用后台接口的时候,会请求两次,如下图的
其实第一次发送的就是preflight request(预检请求),那么这篇文章将讲一下,为什么要发预检请求,什么时候会发预检请求,预检请求都做了什么
一. 为什么要发预检请求我们都知道浏览器的同源策略,就是出于安全考虑,浏览器会限制从脚本发起的跨域http请求,像xmlhttprequest和fetch都遵循同源策略。
浏览器限制跨域请求一般有两种方式:
1. 浏览器限制发起跨域请求
2. 跨域请求可以正常发起,但是返回的结果被浏览器拦截了
一般浏览器都是第二种方式限制跨域请求,那就是说请求已到达服务器,并有可能对数据库里的数据进行了操作,但是返回的结果被浏览器拦截了,那么我们就获取不到返回结果,这是一次失败的请求,但是可能对数据库里的数据产生了影响。
为了防止这种情况的发生,规范要求,对这种可能对服务器数据产生副作用的http请求方法,浏览器必须先使用options方法发起一个预检请求,从而获知服务器是否允许该跨域请求:如果允许,就发送带数据的真实请求;如果不允许,则阻止发送带数据的真实请求。
二. 什么时候发预检请求http请求包括: 简单请求 和 需预检的请求
1. 简单请求简单请求不会触发cors预检请求,“简属于
单请求”术语并不属于fetch(其中定义了cors)规范。
若满足所有下述条件,则该请求可视为“简单请求”:
- 使用下列方法之一:
   - get
   -  head
   -  post
       -  content-type: (仅当post方法的content-type值等于下列之一才算做简单需求)
             -    text/plain
             -    multipart/form-data
             -    application/x-www-form-urlencoded
注意: webkit nightly 和 safari technology preview 为accept
, accept-language
, 和 content-language
首部字段的值添加了额外的限制。如果这些首部字段的值是“非标准”的,webkit/safari 就不会将这些请求视为“简单请求”。webkit/safari 并没有在文档中列出哪些值是“非标准”的,不过我们可以在这里找到相关讨论:require preflight for non-standard cors-safelisted request headers accept, accept-language, and content-language, allow commas in accept, accept-language, and content-language request headers for simple cors, and switch to a blacklist model for restricted accept headers in simple cors requests。其它浏览器并不支持这些额外的限制,因为它们不属于规范的一部分。
2.需预检的请求“需预检的请求”要求必须首先使用options方法发起一个预检请求到服务区,以获知服务器是否允许该实际请求。“预检请求”的使用,可以避免跨域请求对服务器的用户数据产生未预期的影响。
当请求满足下述任一条件时,即应首先发送预检请求:
- 使用了下面任一 http 方法:
   - put
   - delete
   - connect
   - options
   - trace
   - patch
- 人为设置了对 cors 安全的首部字段集合之外的其他首部字段。该集合为:
 - accept
 - accept-language
 - content-language
 - content-type
 - dpr
 - downlink
 - save-data
 - viewport-width
 - width
 - content-type的值不属于下列之一:
     - application/x-www-form-urlencoded
     - multipart/form-data
     - text/plain
如下是一个需要执行预检请求的http请求:
var invocation = new xmlhttprequest(); var url = ' var body = '<?xml version="1.0"?><person><name>arun</name></person>';function callotherdomain(){ if(invocation) { invocation.open('post', url, true); invocation.setrequestheader('x-product', 'h5'); invocation.setrequestheader('content-type', 'application/xml'); invocation.onreadystatechange = handler; invocation.send(body); } } ......
上面的代码使用post请求发送一个xml文档,该请求包含了一个自定义的首部字段(x-product:h5)。另外,该请求的content-type为application/xml。因此,该请求需要首先发起“预检请求”。
1. options /resources/post-here/ 2. http/1.13. host: bar.other4. user-agent: mozilla/5.0 (macintosh; u; 5.intel mac os x 10.5; en-us; rv:1.9.1b3pre) gecko/20081130 minefield/3.1b3pre6. accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.87. accept-language: en-us,en;q=0.58. accept-encoding: gzip,deflate9. accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.710. connection: keep-alive11. origin: http://foo.example12. access-control-request-method: post13. access-control-request-headers: x-pingother, content-type14. http/1.1 200 ok15. date: mon, 01 dec 2008 01:15:39 gmt16. server: apache/2.0.61 (unix)17. access-control-allow-origin: http://foo.example18. access-control-allow-methods: post, get, options19. access-control-allow-headers: x-pingother, content-type20. access-control-max-age: 8640021. vary: accept-encoding, origin22. content-encoding: gzip23. content-length: 024. keep-alive: timeout=2, max=10025. connection: keep-alive26. content-type: text/plain
从上面的报文中可以看到,第1~12行发送了一个使用options方法的预检请求。 options是http/1.1协议中定义的方法,用以从服务器获取更多信息。该方法不会对服务器资源产生影响。遇见请求中同时携带了下面两个首部字段:
access-control-request-method: post access-control-request-headers: x-product
首部字段 access-control-request-method 告知服务器,实际请求将使用 post 方法。首部字段 access-control-request-headers 告知服务器,实际请求将携带两个自定义请求首部字段:x-pingother 与 content-type。服务器据此决定,该实际请求是否被允许。
第14~26行 为预检请求的响应,表明服务器将坚守后续的实际请求。重点看第17~20行:
access-control-allow-origin: http://foo.exampleaccess-control-allow-methods: post, get, options access-control-allow-headers: x-pingother, content-typeaccess-control-max-age: 86400
首部字段 access-control-allow-methods 表明服务器允许客户端使用 post,get 和 options 方法发起请求。
首部字段access-control-allow-headers 表明服务器允许请求中携带字段x-pingother 与content-type。与 access-control-allow-methods一样,access-control-allow-headers的值为逗号分割的列表。
最后,首部字段
access-control-max-age 表明该响应的有效时间为 86400 秒,也就是 24 小时。在有效时间内,浏览器无须为同一请求再次发起预检请求。请注意,浏览器自身维护了一个最大有效时间,如果该首部字段的值超过了最大有效时间,将不会生效。
预检请求完成之后,发送实际请求:
post /resources/post-here/ http/1.1host: bar.otheruser-agent: mozilla/5.0 (macintosh; u; intel mac os x 10.5; en-us; rv:1.9.1b3pre) gecko/20081130 minefield/3.1b3preaccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8accept-language: en-us,en;q=0.5accept-encoding: gzip,deflateaccept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7connection: keep-alivex-pingother: pingpongcontent-type: text/xml; charset=utf-8referer: http://foo.example/examples/preflightinvocation.htmlcontent-length: 55origin: http://foo.examplepragma: no-cachecache-control: no-cache<?xml version="1.0"?><person><name>arun</name></person>http/1.1 200 ok date: mon, 01 dec 2008 01:15:40 gmt server: apache/2.0.61 (unix) access-control-allow-origin: http://foo.example vary: accept-encoding, origin content-encoding: gzip content-length: 235 keep-alive: timeout=2, max=99 connection: keep-alive content-type: text/plain [some gzip'd payload]
以上就是浅谈关于预检请求的详细内容。
其它类似信息

推荐信息