【问题标题】:QtWebEngine: "Not allowed to load local resource" for iframe, how to disable web security?QtWebEngine:iframe的“不允许加载本地资源”,如何禁用网络安全?
【发布时间】:2016-02-17 06:42:20
【问题描述】:

我正在将我的应用程序从 WebKit 移植到 WebEngine(似乎一个更好地呈现 angular-basad html)。 我遇到的问题是我无法启用 QtWebEngine 加载本地 iframe,尽管我已经设置了我发现的所有可能的设置:

mainwindow.cpp 中的代码

view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

最简单的例子是使用基于 WebEngine 的 FancyBrowser (\Examples\Qt-5.4\webenginewidgets\fancybrowser) 并尝试在其中加载本地 html 文件,如下所示:

索引.html:

<html>
<head>
    <title>Hi there</title>
</head>
<body>
    This is a page
    a simple page
    <iframe id="some_idrame" width="0" height="0" style="border: none" src="some_iframe.html" name="target" sandbox="allow-scripts"></iframe>
</body>
</html>

some_iframe.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>La-la-la</title>
</head>
<body>
    Lalala 
</body>
</html>

如果您将环境变量 QTWEBENGINE_REMOTE_DEBUGGING 设置到某个端口,那么您可以打开 127.0.0.1:port 并在控制台中看到此错误:

"Not allowed to load local resource".

我现在真的不知道如何解决这个问题......应该有一些方法可以传递给 WebEngine 类似“--disable-web-security”......

感谢您的帮助!

【问题讨论】:

  • 你是如何在花哨的浏览器中打开它的?你输入了 file:///index.html url 吗?
  • 另外,请粘贴完整的错误。 “不允许加载本地资源:XXXXXXXXXXXXX”它抱怨的文件名称是什么?

标签: javascript html qtwebengine


【解决方案1】:

这个 Qt 论坛链接可以帮助你。 您应该将参数传递给应用程序“--disable-web-security” https://forum.qt.io/topic/60691/not-allowed-to-load-local-resource-for-iframe-how-to-disable-web-security/4

【讨论】:

    【解决方案2】:

    如果您需要通过 WebEngine 加载本地资源,则需要将 --disable-web-security 参数传递给 QApplication,例如:

    char ARG_DISABLE_WEB_SECURITY[] = "--disable-web-security";
    int newArgc = argc+1+1;
    char** newArgv = new char*[newArgc];
    for(int i=0; i<argc; i++) {
        newArgv[i] = argv[i];
    }
    newArgv[argc] = ARG_DISABLE_WEB_SECURITY;
    newArgv[argc+1] = nullptr;
    
    QApplication myApplication(newArgc, newArgv);
    

    【讨论】:

      【解决方案3】:

      另一种选择是也从文件系统加载原始页面。我在从 Qt 的资源系统加载图像时遇到问题,所以我将 QWebEngineView 子类化并创建了这个函数:

      void WebEngineView::setLocalHtml(const QString &html)
      {
          if(html.isEmpty())
          {
              setHtml(QString());
              return;
          }
      
          // Save html to a local file
          QString filePath;
          {
              QTemporaryFile tempFile(QDir::toNativeSeparators(QDir::tempPath() + "/ehr_temp.XXXXXX.html"));
              tempFile.setAutoRemove(false);
              tempFile.open();
              QTextStream out(&tempFile);
              out << html;
      
              filePath = tempFile.fileName();
          }
      
          // delete the file after it has been loaded
          QMetaObject::Connection * const conn = new QMetaObject::Connection;
          *conn = connect(this, &WebEngineView::loadFinished, [filePath, conn](){
              disconnect(*conn);
              delete conn;
      
              QFile::remove(filePath);
          });
      
          load(QUrl::fromLocalFile(filePath));
      }
      

      由于主页也是一个本地文件,这解决了 CORS 安全问题。

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多