LDAP (Lightweight Directory Access Protocol) is a directory information service that allows you to centrally store information and share it across an IP network. In an LDAP service, information is stored in objects called entries, which can contain a variety of data—including authentication information. LDAP entries are often used to store authentication information because data storage is centralized, thereby simplifying maintenance when changes occur.

To configure the driver to use LDAP authentication:
  • Set the ServerName property to specify the name or IP address of the MongoDB server to which you want to connect. For example, myserver.
  • Set the AuthenticationMethod property to plain.
  • Optionally, set the DatabaseName property to specify the name of the database to which you are connecting.
  • Set the User property to specify your user ID.
  • Set the Password property to specify your password.
  • Optionally, specify values for any additional properties you want to configure.
Important: When LDAP authentication is enabled, credentials are passed in clear text. Therefore, you should use LDAP authentication only on servers that are configured for TLS/SSL encryption.
Note: The User and Password properties are not required to be stored in the connection string. They can also be passed separately by the application.

The following examples demonstrate a session with LDAP authentication enabled.

For a connection URL:

Connection conn = DriverManager.getConnection
("jdbc:datadirect:mongodb://myserver:27017;AuthenticationMethod=plain;
       DatabaseName=mydb;user=jsmith;password=secret;");

For a data source:

MongoDBDataSource mds = new MongoDBDataSource();
mds.setDescription("My MongoDB Data Source");
mds.setAuthenticationMethod("plain");
mds.setDatabaseName("mydb");
mds.setServerName("myserver");