spring - 由于标准 header ,CORS 预检请求失败

在调试我遇到的 CORS 问题时,我发现了以下行为。 Chrome 发出以下 OPTIONS 预检请求(由 Chrome 自己用 CURL 重写):

curl -v 'https://www.example.com/api/v1/users' -X OPTIONS -H 'Access-Control-Request-Method: POST' -H 'Origin: http://example.com' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: es-ES,es;q=0.8,en;q=0.6' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36' -H 'Accept: */*' -H 'Referer: http://example.com/users/new' -H 'Connection: keep-alive' -H 'Access-Control-Request-Headers: accept, x-api-key, content-type'

如果满足以下条件,则服务器对此请求的响应:

< HTTP/1.1 403 Forbidden
< Date: Thu, 21 Jul 2016 14:16:56 GMT
* Server Apache/2.4.7 (Ubuntu) is not blacklisted
< Server: Apache/2.4.7 (Ubuntu)
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< Strict-Transport-Security: max-age=31536000 ; includeSubDomains
< X-Frame-Options: SAMEORIGIN
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
< Content-Length: 20
< Keep-Alive: timeout=5, max=100
< Connection: Keep-Alive

作为响应“无效 CORS 请求”的主体。如果我重复删除 header “Access-Control-Request-Method”(并且仅该 header )的请求,则 OPTIONS 请求会成功并具有以下响应:

< HTTP/1.1 200 OK
< Date: Thu, 21 Jul 2016 14:21:27 GMT
* Server Apache/2.4.7 (Ubuntu) is not blacklisted
< Server: Apache/2.4.7 (Ubuntu)
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< Strict-Transport-Security: max-age=31536000 ; includeSubDomains
< X-Frame-Options: SAMEORIGIN 
< Access-Control-Allow-Headers: origin, content-type, accept, x-requested-with, x-api-key
< Access-Control-Max-Age: 60
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
< Access-Control-Allow-Origin: *
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
< Content-Length: 0
< Keep-Alive: timeout=5, max=100
< Connection: Keep-Alive

但是,有问题的 header 是 CORS spec standard header所以它不应该阻止请求成功,对吧?为什么这个 header 会导致这种行为?

当使用 Chrome 发出请求时,如何调整服务器发送的访问控制 header 以使请求正常工作?

顺便说一句,我使用的是 Chrome 36.0,服务器使用的是 Spring Boot,CORS header 由 Spring 管理。

当 Firefox (v47.0) 发出请求时,行为不同,但结果类似。 Firefox 甚至不发送预检请求,它直接发送 POST 请求,该请求收到 403 Forbidden 作为响应。但是,如果我使用“复制为 cURL”选项复制请求,并从终端窗口重复它,它会成功并在响应中发送正确的 CORS header 。

有什么想法吗?

更新:Firefox 确实发送了预检 OPTIONS 请求(如 Live HTTP header 插件所示),但 Firebug 将其屏蔽,因此两种浏览器中的行为完全相同。在这两种浏览器中,“Access-control-request-method” header 是导致请求失败的区别。

最佳答案

经过一番苦苦挣扎,我终于找到了问题所在。我在 Spring 中配置了一个请求映射来处理 OPTIONS 流量,如下所示:

@RequestMapping(value= "/api/**", method=RequestMethod.OPTIONS)
public void corsHeaders(HttpServletResponse response) {
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with");
    response.addHeader("Access-Control-Max-Age", "3600");
}

我不知道默认情况下 Spring 使用 default CORS processor ,它似乎干扰了我的请求映射。删除我的请求映射并添加 @CrossOrigin对相应请求映射的注释解决了这个问题。

https://stackoverflow.com/questions/38507370/

相关文章:

java - 在 Spring Boot 应用程序中安排任务的最佳方法是什么

java - 在 Spring MVC 中将 ContextLoaderListener 添加到 w

java - spring-beans.xsd 没有定义 "local"。为什么?

java - 避免来自 swagger api 的默认基本错误 Controller

java - 如何强制 Spring 容器不返回 bean 的单例实例?

java - Spring MVC - AngularJS - 文件上传 - org.apache.

java - 比较 SpEL 中的枚举

spring - 在 spring 上下文中定义一个字符串

java - 无法将 boolean 值设置为 null

java - 从另一个异步方法调用的 Spring 异步方法