Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /

LDAP (PLAIN SASL)

PLAIN身份验证机制允许您使用轻量级目录访问协议 (LDAP)用户名和密码对MongoDB进行身份验证。LDAP身份验证使用 RFC-4616 中定义的 PLAIN 简单身份验证和安全层 (SASL)。

只有在向MongoDB Atlas或MongoDB Enterprise Advanced进行身份验证时,才能使用此机制。

警告

PLAIN SASL 是一种明文身份验证机制。 在使用 PLAIN SASL 对 MongoDB 进行身份验证时,强烈建议您将 TLS/SSL 与证书验证结合使用。

要详细学习;了解如何为连接启用TLS,请参阅 启用和配置 TLS。

本页上的代码示例使用以下占位符:

  • username:您的 LDAP 用户名

  • password:您的 LDAP 密码

要使用本页上的代码示例,请将这些占位符替换为您自己的值。

要指定 PLAIN身份验证机制,请将 Credential 结构的 mechanism字段设立为 AuthMechanism::Plain

以下代码示例展示了如何使用 PLAIN 机制进行身份验证:

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身份验证机制:

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

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

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

后退

OIDC

在此页面上