QtWebEngine:“;不允许加载本地资源“;对于iframe,如何禁用web安全

QtWebEngine: "Not allowed to load local resource" for iframe, how to disable web security?

本文关键字:iframe web 安全 对于 何禁用 不允许 加载 QtWebEngine 资源      更新时间:2023-09-26

我正在将我的应用程序从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文件,如下所示:

Index.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>

如果您将env var QTWEBONGINE_REMOTE_DEBUGGING设置为某个端口,那么您可以打开127.0.0.1:port并在控制台中看到此错误:

"Not allowed to load local resource".

我现在真的不知道该怎么解决这个问题。。。应该有某种方式将类似"--禁用网络安全"的内容传递给WebEngine。。。

谢谢你的帮助!

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

如果您需要通过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);

另一个选项是从文件系统加载原始页面。我在从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安全问题。