【发布时间】:2020-10-12 14:37:50
【问题描述】:
我有网站www.example.com,它从www.another.com 加载iframe。正在加载的页面包含触发对自身 (www.another.com) 的 AJAX 调用的 HTML 和 JS。这两个网站都使用 HTTPS。
iframe 加载非常好,脚本正在执行,但是,当我单击提交(它是 iframe 的一部分)时,我得到 www.another.com 由于 CSRF 令牌无效而拒绝了我的请求。 iframe 中的表单确实包含一个 token 字段,该字段有一个值(一些哈希)。
当我直接转到www.another.com 时,ajax 调用工作正常。
据我所知,当 ajax 调用到达服务器时,它没有启动会话,因此无法找到要匹配的令牌。
我使用Symfony 4.4 和NelmioCorsBundle 来确保正确的CORS。配置如下所示:
nelmio_cors:
defaults:
allow_credentials: false
origin_regex: false
allow_origin: ['https://www.example.com','https://www.another.com']
allow_methods: ['GET', 'OPTIONS', 'POST']
allow_headers: ['Origin','Referer']
expose_headers: []
max_age: 3600
有什么办法可以解决这个问题吗?
【问题讨论】:
标签: php ajax symfony cross-domain csrf