Overview
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) 进行身份验证
要使用连接字符串指定LDAP (PLAIN)身份验证机制:
将
authMechanismURL 参数分配给值PLAIN(可选)将
authSourceURL 参数分配给值$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)