LDAP (PLAIN) Authentication
Overview
The PLAIN authentication mechanism allows you to use your Lightweight Directory Access Protocol (LDAP) username and password to authenticate to MongoDB. You can use this mechanism only when authenticating to MongoDB Enterprise Advanced.
Tip
PLAIN Authentication
LDAP authentication uses the PLAIN Simple Authentication and Security Layer (SASL) defined in RFC-4616.
Code Placeholders
The code examples on this page use the following placeholders:
<db_username>
: Your LDAP username.<db_password>
: Your LDAP password.<hostname>
: The network address of your MongoDB deployment.<port>
: The port number of your MongoDB deployment. If you omit this parameter, the driver uses the default port number (27017
). You don't need to specify a port when connecting to a MongoDB Atlas cluster.
To use the code examples, replace these placeholders with your own values.
Specify PLAIN Authentication
Select the Connection String or the MongoCredential tab for instructions and sample code for specifying this authentication mechanism:
To specify the LDAP (PLAIN) authentication mechanism by using a connection string, perform the following actions:
Assign the
authMechanism
URL parameter to the valuePLAIN
(optional) Assign the
authSource
URL parameter to the value$external
Note
If you specify the PLAIN
mechanism, you cannot assign
authSource
to any value other than $external
.
The code to instantiate a MongoClient
resembles the following:
MongoClient mongoClient = MongoClients.create("<db_username>:<db_password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN");
To specify the LDAP (PLAIN) authentication mechanism by using the
MongoCredential
class, use the createPlainCredential()
method. The code to instantiate a MongoClient
resembles the following:
MongoCredential credential = MongoCredential.createPlainCredential(<db_username>, "$external", <db_password>); MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) .credential(credential) .build());
API Documentation
To learn more about any of the methods or types discussed on this page, see the following API documentation: