gpt4 book ai didi

C++ Webkit GTK,如何禁用跨源策略?

转载 作者:行者123 更新时间:2023-12-03 07:01:01 25 4
gpt4 key购买 nike

我正在尝试加载 http://google.com在带有“file://”域页面的 iframe 中。当然,我收到了“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 手册对此有一些小技巧。
它被提及为 属性(property)

(allow-file-access-from-file-urls)


但我不知道如何实现我的代码。
编辑:
我在我的代码中添加了这一行
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>

关于C++ Webkit GTK,如何禁用跨源策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63757387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com