问题描述
我正在尝试在iframe中使用“ file://”域页面加载http://google.com。当然我收到“ Google.com不允许”错误。 我已经尝试过反向代理,但是我认为反向代理没有任何意义。
然后,我花了几个小时研究关于禁用或绕过Webkit gtk中的“跨源策略”。
我在https://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html手册页中尝试了一些解决方案
所以,我试图在WebKitSettings中添加此块
WebKitSettings *settings =
webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webview));
webkit_settings_set_allow_file_access_from_file_urls(settings,true);
webkit_settings_set_allow_file_access_from_file_urls(settings,true);
,但不起作用。我仍然无法在iframe中连接到google.com(或任何受cors保护的网站)。
根据我最近的研究,Webkit GTK手册对此有一些小技巧。 它被称为属性
但是我不知道如何实现我的代码。
编辑:
webkit_settings_set_allow_universal_access_from_file_urls(settings,true);
现在我也收到“由于帧将x-frame-options设置为SAMEORIGIN而在框架中拒绝连接”的错误。 如何在webkitgtk中为跨源设置它?
解决方法
正如评论中已经指出的那样,不能绕过CORS政策。
您将无法在iframe中加载任何经过适当配置的网站,以防止出现这种情况。
解决此问题的唯一方法是从您拥有的网站向您要显示的网站发出服务器端请求,为您的网站配置正确的X-Frame-Options并使其返回它从应显示的网站获取的内容。
一种代理,仍然非常容易出错。
我用PHP进行了概念的快速证明。
在https://lucasreta.com/test/google.com,我们有以下脚本,该脚本检索google.com的内容,进行解析并显示它们:
<?php
$url = "https://www.google.com";
$request = curl_init();
curl_setopt_array($request,[
CURLOPT_URL => $url,CURLOPT_RETURNTRANSFER => true,CURLOPT_TIMEOUT => 30,CURLOPT_HTTPHEADER => [
"Content-Type: text/html; charset=UTF-8"
]
]);
$return = curl_exec($request);
curl_close($request);
header('Content-Type: text/html; charset=UTF-8');
# after setting headers,we echo the response
# obtained from the site to display and tweak it
# a bit in order to fix local urls
echo str_replace("=\"/","=\"$url",$return);
在https://lucasreta.com/test/google.com/iframe,我们在iframe中包含上面返回的内容:
<style>
* {font-family: sans-serif}
iframe {width: 90%; height: 85vh;}
</style>
<h1>Google in an iframe</h1>
<iframe src="../"></iframe>