2

我正在尝试使用“file://”域页面在 iframe中加载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 手册对此有一些小技巧。它被称为财产

(允许文件访问来自文件 urls)

但我不知道如何实现我的代码。

编辑:

我在我的代码中添加了这一行

webkit_settings_set_allow_universal_access_from_file_urls(settings,true);

现在我也收到“在框架中拒绝连接,因为它将 X-Frame-Options 设置为 SAMEORIGIN”错误。我如何在 webkitgtk 中为跨源设置它?

4

1 回答 1

2

正如评论中已经指出的那样,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>
于 2020-10-06T21:25:43.663 回答