对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

企业身份验证机制

在本指南中,您可以了解如何使用 MongoDB Enterprise Edition 中提供的身份验证机制对 MongoDB 进行身份验证。 连接到 MongoDB 时,可以使用身份验证机制在驱动程序和服务器之间建立信任。

Rust 驱动程序支持使用LDAP (PLAIN)企业身份验证机制向轻量级目录访问协议 (LDAP) 服务器进行身份验证。

注意

GSSAPI/Kerberos 身份验证

The driver does not support the GSSAPI/Kerberos authentication mechanism, but you can use other methods to authenticate this way. To learn more about these methods, see Kerberos Authentication in the Server manual.

提示

要使用 MongoDB Community Edition 中提供的身份验证机制进行 MongoDB 身份验证,请参阅身份验证机制指南。

要了解有关连接到 MongoDB 部署的更多信息,请参阅连接指南。

要选择特定的身份验证机制,请在连接字符串的选项或Credential结构中指定该机制、您的档案和其他必要信息。

您可以使用目录服务器用户名和密码向轻量级目录访问协议 (LDAP) 服务器进行身份验证。

身份验证机制的名称是 PLAIN 而不是LDAP ,因为该机制使用 RFC-4616。 中定义的 PLAIN 简单身份验证和安全层 (SASL)。

警告

此身份验证机制以明文形式将密码发送到服务器。 仅在对连接启用 TLS 后使用此机制,以提高安全性并减少应用程序中的漏洞。

To learn more, see TLS/SSL (Transport Encryption) in the Server manual.

要指定PLAIN身份验证机制,请将Credential结构的mechanism字段设置为AuthMechanism::Plain 。 此示例使用以下占位符指定身份验证机制:

  • username:您的 LDAP 用户名

  • password:您的 LDAP 密码

let plain_cred = Credential::builder()
.username("<username>".to_string())
.password("<password>".to_string())
.mechanism(AuthMechanism::Plain)
.source("$external".to_string())
.build();
client_options.credential = Some(plain_cred);
let client = Client::with_options(client_options)?;

注意

身份验证数据库

由于您的档案存储在 MongoDB 外部,因此您必须使用$external数据库进行身份验证。 The source field of the Credential struct defaults to $external, so you can omit this field.

或者,您可以通过将authMechanism连接字符串选项的值设置为PLAIN来使用连接字符串 URI 进行身份验证。 此示例演示如何使用以下占位符在连接字符串 URI 中指定PLAIN身份验证机制:

  • username:您的 LDAP 用户名

  • password:您的 LDAP 密码

  • hostname:你的 MongoDB Server 的网络地址

let uri = "mongodb://<username>:<password>@<hostname>/?authSource=$external&authMechanism=PLAIN";

要了解有关本指南中概念的更多信息,请参阅以下文档:

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: