Iva*_*van 5 iframe ruby-on-rails x-frame-options ruby-on-rails-6
我希望允许任何人通过 iFrame 嵌入我的 Rails 网站。但我只想打开一些特定的路线,例如/emded/entity1/:entity1_id和/emded/entity2/:entity2_id。
例如,Youtube 就是这么做的。我可以嵌入以下代码:
<iframe width="420" height="315"src="https://www.youtube.com/embed/tgbNymZ7vqY">
</iframe>
Run Code Online (Sandbox Code Playgroud)
但如果我将src属性更改为 just https://www.youtube.com/,这个 iframe 将不会显示任何内容。
在 Rails 中做同样的事情的方法是什么?
我尝试遵循为 Rails 4 找到的指南:http ://jerodsanto.net/2013/12/rails-4-let-specific-actions-be-embedded-as-iframes/
我在应用程序控制器中创建了一个方法:
def allow_iframe
response.headers.delete "X-Frame-Options"
end
Run Code Online (Sandbox Code Playgroud)
然后我将其添加到控制器中,并使用显示 iframe 内容(页面)的操作。after_action :allow_iframe,仅: :show_page_in_iframe
这是控制器操作本身:
def show_page_in_iframe
...
respond_to do |format|
format.html { render layout: 'iframe' }
end
end
Run Code Online (Sandbox Code Playgroud)
然后我将这些更改部署到生产环境,将带有相应 URL 的 iframe 嵌入到“localhost”页面。
<iframe src="https://www.example.com/entity1/embed/66efdc3e-7cb7-436d-98e8-63275fa74ebd" height="500" width="100%" style="border:0"></iframe>
Run Code Online (Sandbox Code Playgroud)
但在 Chrome 控制台中我可以看到Refused to display 'https://www.example.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.错误消息。
我在这里做错了什么?这种方法对于 Rails 6 仍然可行吗?
更新 1.
经过一些调试后localhost,我发现只有当我response.headers.delete "X-Frame-Options"在控制器操作中精确地包含该行时,才会删除“X-Frame-Options”值。但将这些更改部署到生产 (Heroku) 并没有解决问题。
更新2。
此外,我尝试设置ALLOWALL的值X-Frame-Options- 它也没有帮助我。
更新3。
另外,我尝试过X-Frame-Options另一种删除方式:response.headers.except! 'X-Frame-Options'- 它也不起作用。
您是否尝试过设置frame_ancestors?
# config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy do |policy|
policy.frame_ancestors :self, "*"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2583 次 |
| 最近记录: |