Overview
在本指南中,您可以了解如何使用 MongoDB Enterprise Edition 中提供的身份验证机制对 MongoDB 进行身份验证。 连接到 MongoDB 时,可以使用身份验证机制在驱动程序和服务器之间建立信任。
Rust 驱动程序支持使用 LDAP (PLAIN)企业身份验证机制向轻量级目录访问协议 (LDAP) 服务器进行身份验证。
注意
GSSAPI/Kerberos 身份验证
驱动程序不支持 GSSAPI/Kerberos 身份验证机制,但您可以使用其他方法进行身份验证。 要了解有关这些方法的更多信息,请参阅Kerberos MongoDB Server手册中的 身份验证 。
要选择特定的身份验证机制,请在连接字符串的选项或 Credential结构中指定该机制、您的档案和其他必要信息。
LDAP 身份验证 (PLAIN)
您可以使用目录服务器用户名和密码向轻量级目录访问协议 (LDAP) 服务器进行身份验证。
身份验证机制的名称是 PLAIN 而不是LDAP ,因为该机制使用 RFC-4616 中定义的 PLAIN 简单身份验证和安全层 (SASL)。
警告
例子
要指定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";
更多信息
要了解有关本指南中概念的更多信息,请参阅以下文档:
MongoDB ServerLDAPMongoDB MongoDB Server手册中的 MongoDB Server 代理身份验证的支持
连接选项指南
手册中的 连接字符串MongoDB Server
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: