Symfony 4.1-CORS问题

时间:2018-11-06 14:23:30

标签: angular symfony cors

我的symfony 4.1 API有一些问题:

我正在使用带有角度的httpclient的离子应用程序来使用我的API。

我的问题是CORS标头,尤其是Access-Control-Allow-Methods

我的CORS遇到了问题,因为我的API和应用程序的来源不同,因此我的安装nelmio/cors-bundle来处理CORS。

我的nelmio_cors.yaml如下:

nelmio_cors:
    paths:
        '^/api/':
            origin_regex: true
            allow_origin: ['*']
            allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
            allow_headers: ['Content-Type', 'Authorization']
            max_age: 3600

这实际上适用于此后我使用的所有方法:

  • 发布请求[确定]
  • 获取请求[确定]
  • 删除请求[确定]

现在,我想向我的API添加PATCH路由。我已经用Postman测试了控制器,我的工作像个魅力。现在,当我从角度服务查询相同的路线时:

        return this.http.patch(this.url + '/api/users/' + userId,
                               dataToPatch, 
                               this.authHeaders)
                        .map(response => response.json());

控制台记录以下内容:

Failed to load http://symfony.local/api/users/1: Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.

在这里您可以看到请求的响应标头:

Access-Control-Allow-Headers: authorization,content-type
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Allow: POST, GET, OPTIONS, PUT, DELETE
Cache-Control: no-cache, private
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Tue, 06 Nov 2018 14:02:51 GMT
Server: nginx
Transfer-Encoding: chunked
X-Powered-By: PHP/7.2.11

如您所见,如果我正确理解CORS标头,则不允许使用PATCH方法,但是为什么在使用postman来使用API​​时它可以起作用。

我还安装了chrome的Allow-Control-Allow-Origin:*扩展名,但在那里也没有成功...

我正在使用https://github.com/ikamikaz3/docker-symfony,因为我的堆栈可能来自那里吗? (可能在某处配置错误?)

如果需要,我可以提供更多代码,但这对我来说似乎是一个愚蠢的错误...

编辑1:

从chrome卸载Allow-Control-Allow-Origin:*后,我在登录时得到以下提示

Failed to load http://symfony.local/login_check: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'http://localhost:8100' is therefore not allowed access.

编辑2:

在我的nginx容器中使用以下内容更新了我的symfony.conf之后,我设法使API正常工作,虽然PATCH仍然损坏,但我认为我可以做到<3

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }
    if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    }

1 个答案:

答案 0 :(得分:0)

我设法使它起作用,所以这是我的参考方法:

  1. 删除nelmio / cors-bundle(因为我们直接使用NGINX处理CORS),因为它引起了类似2 Allow-Control-Allow-Origin标头字段的冲突。

  2. 在nginx.conf中添加我想要的方法(其余配置与我的原始帖子一样)

    if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Methods' 'HTTP_VERBS, SEPARATED, WITH, COMMAS';

  3. 重建我的docker堆栈docker-compose build

  4. 部署docker-compose up -d
  5. 享受CORS的支持

您可以在http://github.com/ikamikaz3/docker-symfony上找到我的maxpou的docker-symfony分支,以获取更多深度信息(配置文件等)。 其中包含一个带有ELK / Kibana,Symfony flex堆栈,PhpMyAdmin(WIP),以及现在具有CORS支持的NGINX!

相关问题