Symfony 3 HTTP 缓存和跨源 Headers 覆盖

Symfony 3 HTTP Cache and Cross Origin Headers overwrite

我有一个使用 Symfony 3 编写的后端 API 的前端 AngularJS 应用程序。我正在使用 Symfony HTTP 缓存来加速我的一些繁重的响应。

我正处于测试阶段,所以我的后端 API 添加了 Access-Control-Allow-Headers:"*" 所有回复。

但是,缓存的响应存在问题。在它们的 header 中,Access-Control-Allow-Headers:"*"Access-Control-Allow-Headers:"example.com" 覆盖,其中 example.com 是在缓存之前请求资源的客户端地址。

因此,如果我在地址 www.example.com 而不是地址 example.com 打开我的前端应用程序,我会在 firebug

中看到以下错误
XMLHttpRequest cannot load https://backend.com/tests/all. 
The 'Access-Control-Allow-Origin' header has a value 'http://example.com' 
that is not equal to the supplied origin. 
Origin 'http://www.example.com' is therefore not allowed access. 

我正在使用 NelmioCorsBundle,它为我的 Symfony3 应用程序添加了 CORS headers 支持。

尝试在您的配置中设置 forced_allow_origin_value: *

 nelmio_cors:
    defaults:
        …
        forced_allow_origin_value: *

根据 the configuration docs:

By default, the Access-Control-Allow-Origin response header value is the Origin request header value (if it matches the rules you've defined with allow_origin), so it should be fine for most of use cases. If it's not, you can override this behavior by setting the exact value you want using forced_allow_origin_value.

所以要发送 Access-Control-Allow-Origin: *,你想要的确切值就是 *.