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

LDAP (PLAIN SASL) 身份验证

The PLAIN authentication mechanism allows you to use your Lightweight Directory Access Protocol (LDAP) username and password to authenticate to MongoDB. LDAP authentication uses the PLAIN Simple Authentication and Security Layer (SASL) defined in RFC-4616.

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

重要

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

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

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

  • LDAP username - 您的 LDAP 用户名

  • password - LDAP 用户的密码

  • hostname - 可供客户端访问的 MongoDB 服务器的网络地址

  • port - MongoDB Server 的端口号

要使用连接字符串指定LDAP (PLAIN)身份验证机制:

  • authMechanism URL 参数分配给值PLAIN

  • (可选)authSource URL 参数分配给值$external

注意

如果指定PLAIN 机制,则不能将authSource 分配给$external 以外的任何值。

用于实例化MongoClient的代码应类似于以下内容:

val connectionString = ConnectionString("<LDAP username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
val mongoClient = MongoClient.create(connectionString)

要使用 MongoCredential 类指定LDAP (PLAIN)身份验证机制,请使用 createPlainCredential() 方法。用于实例化 MongoClient 的代码应类似于以下内容:

val credential = MongoCredential.createPlainCredential("<LDAP username>", "$external", "<password>".toCharArray())
val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder ->
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
}
.credential(credential)
.build()
val mongoClient = MongoClient.create(settings)