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.
You can use this mechanism only when authenticating to MongoDB Atlas or MongoDB Enterprise Advanced.
Important
PLAIN SASL is a clear-text authentication mechanism. Use TLS/SSL with certificate validation when using PLAIN SASL to authenticate to MongoDB.
To learn more about how to enable TLS for your connection, see the Configure Transport Layer Security (TLS) guide.
Code Placeholders
The code examples on this page use the following placeholders:
<ldap_username>: Your LDAP username<password>: Your LDAP password<hostname>: The network address of your MongoDB server, accessible by your client<port>: The port number of your MongoDB server
Specify LDAP (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, assign the following URL parameters:
authMechanism: Set toPLAIN(Optional)
authSource: Set to$external
Note
If you specify the PLAIN mechanism, you cannot assign authSource to any value other than $external.
The following example uses a connection string to instantiate a MongoClient and specify LDAP authentication:
val mongoClient = MongoClient("mongodb://<ldap_username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
To specify the LDAP (PLAIN) authentication mechanism by using the MongoCredential class, use the createPlainCredential() method.
The following example uses the createPlainCredential() method to instantiate a MongoClient and specify LDAP authentication:
val credential = MongoCredential.createPlainCredential( "<ldap_username>", "$external", "<password>".toCharArray()) val mongoClient = MongoClient(MongoClientSettings .builder() .applyToClusterSettings(builder => builder.hosts(Collections.singletonList( ServerAddress("<hostname>", <port>)))) .credential(credential) .build())
Additional Information
To learn more about authenticating to MongoDB, see Authentication on Self-Managed Deployments.
To learn more about creating a MongoClient object by using the Scala driver, see the Create a MongoClient guide.
API Documentation
To learn more about the classes and methods for authenticating your application with the Scala driver, see the following API documentation: