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()
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 类通常用于QNetworkAccessManagerQAbstractSocketauthenticationRequired() 和proxyAuthenticationRequired() 信号中。在访问需要身份验证的服务时,该类提供了一种向套接字传回所需身份验证信息的方法。

QAuthenticator 支持以下验证方法:

  • 基本
  • NTLM 第 2 版
  • 数字-MD5
  • SPNEGO/Negotiate

选项

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

下表列出了已知的输入选项和可接受的输出选项。输入选项列表并不详尽,因为服务器可能会随时添加其他信息。不过,输出选项列表是详尽无遗的,不会处理或向服务器发送未知选项。

基本选项

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

基本身份验证机制不支持传出选项。

NTLM 版本 2

NTLM 身份验证机制目前不支持传入或传出选项。在 Windows 上,如果没有设置user ,则会在本地系统上搜索 domain\user 凭据,以启用单点登录功能。

摘要-MD5

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

Digest-MD5 身份验证机制不支持传出选项。

SPNEGO/协商

选项方向类型说明
spn传出QString提供自定义 SPN。

此身份验证机制目前不支持传入选项。

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

其他操作系统使用 GSSAPI 库。为此,需要设置 KDC,并从 KDC 获取凭据。后端始终使用HTTPS@<hostname> 作为 SPN。

另请参阅 QSslSocket

成员函数文档

QAuthenticator::QAuthenticator()

构造一个空身份验证对象。

QAuthenticator::QAuthenticator(const QAuthenticator &other)

构造other 的副本。

[noexcept] QAuthenticator::~QAuthenticator()

销毁对象。

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

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