Overview
MongoDB Enterprise Edition includes authentication mechanisms that aren't available in MongoDB Community Edition. In this guide, you can learn how to authenticate to MongoDB by using these authentication mechanisms. To learn about the other authentication mechanisms available in MongoDB, see the Authentication Mechanisms guide.
Specify an Authentication Mechanism
You can specify your authentication mechanism and credentials when connecting to MongoDB by using either of the following:
Connection string
MongoCredentialfactory method
A connection string (also known as a connection URI) specifies how to connect and authenticate to your MongoDB cluster.
To authenticate by using a connection string, include your settings in your connection string, then pass it to the MongoClients.create() method to instantiate your MongoClient. Select the Connection String tab in the following sections to see the syntax for authenticating by using a connection string.
You can also use the MongoCredential class to specify your authentication details. The MongoCredential class contains static factory methods that construct instances containing your authentication mechanism and credentials. When you use the MongoCredential helper class, use the MongoClientSettings.Builder class to configure your connection settings. Select the MongoCredential tab in the following sections to see the syntax for authenticating using a MongoCredential.
Important
The Java Reactive Streams driver does not support UnixServerAddress objects or domain socket connections. To use a domain socket to connect, use the Java Sync driver. Otherwise, use a ServerAddress object to connect from the Java Reactive Streams driver.
Mechanisms
Kerberos (GSSAPI)
The Generic Security Services API (GSSAPI) authentication mechanism allows you to authenticate to a Kerberos service by using your principal name.
The following sections contain code examples that use the following placeholders:
username: your URL-encoded principal name, such as"username%40REALM.ME"hostname: network address of your MongoDB deployment that your client can accessport: port number of your MongoDB deployment
Select the Connection String or MongoCredential tabs to see the corresponding syntax.
The following example authenticates to GSSAPI by using a connection string:
MongoClient mongoClient = MongoClients .create("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI");
To specify the GSSAPI authentication mechanism by using the MongoCredential class, call the createGSSAPICredential() method, as shown in the following example:
MongoCredential credential = MongoCredential.createGSSAPICredential("<username>"); MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) .credential(credential) .build());
To acquire a Kerberos ticket, the GSSAPI Java libraries require you to specify the realm and Key Distribution Center (KDC) system properties. You can set these settings as shown in the following example:
java.security.krb5.realm=MYREALM.ME java.security.krb5.kdc=mykdc.myrealm.me
You might need to specify one or more of the following additional MongoCredential mechanism properties, depending on your Kerberos setup:
SERVICE_NAMECANONICALIZE_HOST_NAMEJAVA_SUBJECTJAVA_SASL_CLIENT_PROPERTIESJAVA_SUBJECT_PROVIDER
Important
You can specify the following GSSAPI properties only through the MongoCredential class:
JAVA_SUBJECTJAVA_SASL_CLIENT_PROPERTIESJAVA_SUBJECT_PROVIDER
Select the MongoCredential tab to learn how to specify these properties.
To specify the GSSAPI additional properties, include the property in the connection string as a URL parameter in the format: <PROPERTY_NAME>:<value>.
The following example authenticates to GSSAPI and specifies additional properties:
MongoClient mongoClient = MongoClients .create("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService");
To specify the GSSAPI additional properties, call the withMechanismProperty() method on your MongoCredential instance, and pass the property name and value as parameters. Use the property name constants defined in the MongoCredential class:
Select the SERVICE_NAME_KEY or JAVA_SUBJECT_KEY tab to see how to specify the corresponding property:
MongoCredential credential = MongoCredential .createGSSAPICredential("<username>"); credential = credential .withMechanismProperty(MongoCredential.SERVICE_NAME_KEY, "<myService>");
LoginContext loginContext = new LoginContext(<LoginModule implementation from JAAS config>); loginContext.login(); Subject subject = loginContext.getSubject(); MongoCredential credential = MongoCredential .createGSSAPICredential("<username>"); credential = credential .withMechanismProperty(MongoCredential.JAVA_SUBJECT_KEY, subject);
By default, the Java Reactive Streams driver caches Kerberos tickets by MongoClient instance. If your deployment frequently creates and destroys MongoClient instances, you can change the default Kerberos ticket caching behavior to cache by process to improve performance.
To cache Kerberos tickets by process, you must use the MongoCredential authentication mechanism, because the connection string authentication mechanism does not support the JAVA_SUBJECT_PROVIDER mechanism property. Select the MongoCredential tab to learn how to cache Kerberos tickets by process.
To cache Kerberos tickets by process, specify the JAVA_SUBJECT_PROVIDER mechanism property and provide a KerberosSubjectProvider in your MongoCredential instance, as shown in the following example:
/* All MongoClient instances sharing this instance of KerberosSubjectProvider will share a Kerberos ticket cache */ String myLoginContext = "myContext"; MongoCredential credential = MongoCredential .createGSSAPICredential(<username>); /* Login context defaults to "com.sun.security.jgss.krb5.initiate" if unspecified in KerberosSubjectProvider */ credential = credential .withMechanismProperty(MongoCredential.JAVA_SUBJECT_PROVIDER_KEY, new KerberosSubjectProvider(myLoginContext));
LDAP (PLAIN)
You can authenticate to a Lightweight Directory Access Protocol (LDAP) server by using your directory server username and password.
Tip
The authentication mechanism is named PLAIN instead of LDAP since it authenticates using the PLAIN Simple Authentication and Security Layer
(SASL) defined in RFC-4616.
The following sections contain code examples that use the following placeholders:
ldap_username: your LDAP usernameldap_password: your LDAP user's passwordhostname: network address of your MongoDB deployment that your client can accessport: port number of your MongoDB deployment
Select the Connection String or MongoCredential tabs to see the corresponding syntax.
MongoClient mongoClient = MongoClients .create("<ldap_username>:<ldap_password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN");
To specify the LDAP (PLAIN) authentication mechanism by using the MongoCredential class, call the createPlainCredential() method, as shown in the following example:
MongoCredential credential = MongoCredential .createPlainCredential(<ldap_username>, "$external", <ldap_password>); MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) .credential(credential) .build());