【问题标题】:CORS requests with preflight on CouchDB在 CouchDB 上进行预检的 CORS 请求
【发布时间】:2018-11-27 13:53:27
【问题描述】:

我们正在尝试向 CouchDB 发送 HTTP 跨域请求。

为了实现这一点,我们使用以下设置设置了 CouchDB(灵感来自 add-cors-to-couchdb tool):

[HTTPD]
enable_cors = true

[CORS]
origins = *
credentials = true
methods = GET, PUT, POST, HEAD, DELETE
headers = accept, authorization, content-type, origin, referer, x-csrf-token

并编写了类似的代码(当然不是硬编码):

<html>
  <body>
    <script>
      fetch("http://acme.org:5984/mydb/323958d9b42be0aaf811a3c96b4e5d9c", {
        method: 'DELETE',
        headers: {'If-Match': "1-0c2099c9793c2f4bf3c9fd6751e34f95"}
      }).then(x => {
        if (x.ok) return x.json().then(console.log);
        console.error(x.statusText);
      });
    </script>
   </body>
 </html>

虽然它适用于GETPOST,但我们在DELETE 上得到405 Method not allowed。浏览器告诉预检响应(对OPTIONS 请求)不成功,而服务器指示{"error":"method_not_allowed","reason":"Only DELETE,GET,HEAD,POST,PUT,COPY allowed"}

我们对 CouchDB 2.1.1 和 1.6.1 都进行了尝试。我们还尝试将origins: * 替换为origins: http://localhost:8080(其中8080​​ 是为上述HTML 提供服务的端口)。我们还尝试将credentials 设置为false

【问题讨论】:

  • 请不要使用origin: *,因为它允许CSRF漏洞利用!

标签: rest cors couchdb http-status-code-405 preflight


【解决方案1】:

a comment by @Ingo Radatz 到一个相关问题,我终于知道请求中使用的 EVERY 标头必须包含在 CouchDB CORS 设置中。

就我个人而言,我必须在接受的标头中包含if-match

[HTTPD]
enable_cors = true

[CORS]
origins = *
methods = GET, PUT, POST, HEAD, DELETE
headers = accept, authorization, content-type, origin, referer, if-match

【讨论】:

  • 非常感谢@Aurélien 的这个提示,我终于知道如何使用 couchdb 配置我的 vue 应用了。
  • @IanWootten,我很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2021-03-27
  • 2014-10-31
  • 2015-09-29
  • 2015-08-10
  • 2013-10-24
  • 2015-11-26
相关资源
最近更新 更多