本页内容

QWebSocketCorsAuthenticator Class

QWebSocketCorsAuthenticator 类为跨源请求(CORS)提供了一个身份验证对象。更多内容...

头文件: #include <QWebSocketCorsAuthenticator>
CMake: find_package(Qt6 REQUIRED COMPONENTS WebSockets)
target_link_libraries(mytarget PRIVATE Qt6::WebSockets)
qmake: QT += websockets

公共函数

QWebSocketCorsAuthenticator(const QString &origin)
QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other)
QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other)
~QWebSocketCorsAuthenticator()
bool allowed() const
QString origin() const
void setAllowed(bool allowed)
void swap(QWebSocketCorsAuthenticator &other)
QWebSocketCorsAuthenticator &operator=(QWebSocketCorsAuthenticator &&other)
QWebSocketCorsAuthenticator &operator=(const QWebSocketCorsAuthenticator &other)

详细说明

QWebSocketCorsAuthenticator 类用于originAuthenticationRequired() 信号中。该类提供了一种将所需信息回传给QWebSocketServer 的方式。它使应用程序能够精细控制允许和不允许的源 URL。默认情况下,所有源均被接受。 为了实现精细控制,应用程序需将originAuthenticationRequired() 信号连接到一个槽。当源(QWebSocketCorsAuthenticator::origin())被接受时,它会调用QWebSocketCorsAuthenticator::setAllowed(true)

注意: 当通过非浏览器客户端访问服务器时,检查 源头意义不大,因为该客户端可以设置任意源头标头。对于浏览器客户端,服务器“应”检查源头的有效性。

另请参阅 《WebSocket 安全考虑事项》和QWebSocketServer 。

成员函数文档

[explicit] QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QString &origin)

使用给定的origin 构建一个新的QCorsAuthenticator对象。

注意:默认情况下, allowed() 返回 true。这意味着默认情况下会接受所有来源。

[explicit] QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(const QWebSocketCorsAuthenticator &other)

创建other 的副本。

[noexcept] QWebSocketCorsAuthenticator::QWebSocketCorsAuthenticator(QWebSocketCorsAuthenticator &&other)

通过“move”操作构造一个 QWebSocketCorsAuthenticator,使其指向与other 所指向的相同对象。

[noexcept] QWebSocketCorsAuthenticator::~QWebSocketCorsAuthenticator()

销毁该对象。

bool QWebSocketCorsAuthenticator::allowed() const

如果源被允许,则返回 true;否则返回 false。

注意: 默认情况下 ,所有来源均被接受。

另请参阅 setAllowed()。

QString QWebSocketCorsAuthenticator::origin() const

返回该身份验证器所处理的来源。

void QWebSocketCorsAuthenticator::setAllowed(bool allowed)

允许或拒绝该源。将allowed 设置为true,将接受来自该源的连接请求。

将allowed 设置为false将拒绝该连接请求。

注意:默认情况下 ,所有源均被接受。

另请参阅 allowed()。

[noexcept] void QWebSocketCorsAuthenticator::swap(QWebSocketCorsAuthenticator &other)

将other 与该身份验证器进行交换。

此操作速度极快,且绝不会失败。

[noexcept] QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator=(QWebSocketCorsAuthenticator &&other)

将 `other ` 移动并赋值给该实例。

QWebSocketCorsAuthenticator &QWebSocketCorsAuthenticator::operator=(const QWebSocketCorsAuthenticator &other)

将other 分配给此身份验证器对象。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.