创建可以iFrame mysite.com的网站白名单

时间:2013-12-04 06:12:49

标签: ruby-on-rails iframe

我拥有一个人们创建东西并将其嵌入其网站的网站。我们称之为“mysite.com”。

用户可以创建类似“mysite.com/1”的页面并将其嵌入其网站“acceptable.com”。如何阻止像“forbidden.com”这样的其他网站在iframe中显示“mysite.com/1”?

tl; dr如何为尝试iframe我自己网站的网站制作“白名单”?

1 个答案:

答案 0 :(得分:2)

通常,您可以在服务器响应中发送X-Frame-Options标头,该标头提供以下选项:

DENY
The page cannot be displayed in a frame, regardless of the site attempting to do so.

SAMEORIGIN
The page can only be displayed in a frame on the same origin as the page itself.

ALLOW-FROM uri
The page can only be displayed in a frame on the specified origin.

https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options

要在rails中执行此操作,您可以为所有页​​面设置(source

config.action_dispatch.default_headers.merge!({'X-Frame-Options' => '[OPTION HERE]'})

或仅针对特定网页,请参阅How to override X-Frame-Options for a controller or action in Rails 4