本页内容

QAuthenticator Class

QAuthenticator 类提供了一个身份验证对象。更多内容...

头文件: #include <QAuthenticator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network

注意:该类中的所有函数均为可重入函数。

公共函数

QAuthenticator()
QAuthenticator(const QAuthenticator &other)
~QAuthenticator()
(since 6.11) void clear()
bool isNull() const
QVariant option(const QString &opt) const
QVariantHash options() const
QString password() const
QString realm() const
void setOption(const QString &opt, const QVariant &value)
void setPassword(const QString &password)
void setUser(const QString &user)
QString user() const
bool operator!=(const QAuthenticator &other) const
QAuthenticator &operator=(const QAuthenticator &other)
bool operator==(const QAuthenticator &other) const

详细说明

QAuthenticator类通常用于QNetworkAccessManager 和QAbstractSocket 的authenticationRequired()和proxyAuthenticationRequired()信号中。该类提供了一种方法,在访问需要身份验证的服务时,将所需的身份验证信息回传给套接字。

QAuthenticator 支持以下身份验证方法:

  • Basic
  • NTLM 2.0
  • 摘要-MD5
  • SPNEGO/Negotiate

选项

除了身份验证所需的用户名和密码外,QAuthenticator对象还可以包含其他选项。可以使用options()函数查询服务器发送的传入选项;可以使用setOption()函数设置传出选项,以便在身份验证计算中进行处理。 可接受和提供的选项取决于身份验证类型(参见 method())。

下表列出了已知的传入选项以及被接受的传出选项。传入选项列表并非详尽无遗,因为服务器可能随时包含其他信息。然而,传出选项列表是详尽的,系统不会处理任何未知选项,也不会将其发回给服务器。

基本

选项方向类型描述
realm传入QString包含身份验证的领域,与realm() 中的相同

基本身份验证机制不支持任何发出的选项。

NTLM 2.0

NTLM 身份验证机制目前不支持任何入站或出站选项。在 Windows 系统上,如果未设置user ,系统将在本地系统中搜索域\user 的凭据,以启用单点登录功能。

摘要-MD5

选项方向类型描述
realm传入QString包含身份验证的域,与realm()中的域相同

Digest-MD5 身份验证机制不支持任何发出的选项。

SPNEGO/Negotiate

选项方向类型描述
spn出站QString提供自定义 SPN。

该身份验证机制目前不支持任何入站选项。

在使用 SSPI 库时,spn 属性在 Windows 客户端上使用。如果未设置该属性,将使用默认的 SPN。Windows 上的默认 SPN 为HTTP/<hostname> 。

其他操作系统使用 GSSAPI 库。在此情况下,预计已配置 KDC,且可从中获取凭据。后端始终使用HTTPS@<hostname> 作为 SPN。

安全注意事项

QAuthenticator 内部使用通用数据类型(QString 、QByteArray )存储用户名和密码等凭据,这些数据类型无法保证在对象销毁时能安全地从内存中清除其内容。在 QAuthenticator 对象被销毁或清空后,凭据信息可能会残留在已释放的堆页、核心转储、交换文件或进程内存中。

在协商基本认证时,凭据会采用 Base64 编码进行传输,而这种编码极易被逆向解码。基本认证仅应通过 TLS 加密连接使用。QAuthenticator 不会强制执行此要求;在使用基本认证时,应用程序有责任确保传输安全。

QAuthenticator 会根据服务器提供的认证方法,按照固定的内部优先级顺序(Negotiate(Kerberos/SPNEGO)、NTLM、Digest-MD5 和 Basic)选择最强的认证方法。 目前,应用程序无法影响此选择,也无法限制可接受的认证方法。如果服务器或代理仅提供弱认证方法,系统将直接使用该方法,且不会发出通知。

对凭据安全有严格要求的应用程序,在决定如何以及在何处使用 QAuthenticator 时,应将此因素纳入考量。

另请参阅 QSslSocket 。

成员函数文档

QAuthenticator::QAuthenticator()

创建一个空的身份验证对象。

QAuthenticator::QAuthenticator(const QAuthenticator &other)

创建other 的副本。

[noexcept] QAuthenticator::~QAuthenticator()

销毁该对象。

[since 6.11] void QAuthenticator::clear()

清除所有凭据,并将对象重置为默认的未初始化状态。

该函数于 Qt 6.11 中引入。

bool QAuthenticator::isNull() const

如果对象尚未初始化,则返回true 。如果调用了非const成员函数,或者内容是从另一个已初始化的QAuthenticator 对象构建或复制的,则返回false 。

QVariant QAuthenticator::option(const QString &opt) const

如果服务器设置了opt 选项,则返回与该选项相关的值。有关传入选项的更多信息,请参阅Options section 。如果未找到opt 选项,则将返回一个无效的QVariant 。

另请参阅 setOption()、options() 和QAuthenticator options 。

QVariantHash QAuthenticator::options() const

通过解析服务器响应,返回在此QAuthenticator 对象中设置的所有传入选项。有关传入选项的更多信息,请参阅Options section 。

另请参阅 option() 和QAuthenticator options 。

QString QAuthenticator::password() const

返回用于身份验证的密码。

另请参阅 setPassword()。

QString QAuthenticator::realm() const

返回需要进行身份验证的域。

void QAuthenticator::setOption(const QString &opt, const QVariant &value)

将发送选项opt 设置为值value 。有关发送选项的更多信息,请参阅Options section 。

另请参阅 options()、option() 以及QAuthenticator options 。

void QAuthenticator::setPassword(const QString &password)

设置用于身份验证的password 。

另请参阅 password() 和QNetworkAccessManager::authenticationRequired()。

void QAuthenticator::setUser(const QString &user)

设置用于身份验证的user 。

另请参阅 user() 和QNetworkAccessManager::authenticationRequired()。

QString QAuthenticator::user() const

返回用于身份验证的用户。

另请参阅 setUser()。

bool QAuthenticator::operator!=(const QAuthenticator &other) const

如果该身份验证器与other 不一致,则返回true ;否则返回false 。

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

将other 中的内容分配给此身份验证器。

bool QAuthenticator::operator==(const QAuthenticator &other) const

如果该身份验证器与other 完全相同,则返回true ;否则返回false 。

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