Overview
In the X.509 authentication mechanism, the server and client use the TLS protocol to exchange X.509 public-key certificates. You can use this mechanism to authenticate to MongoDB Atlas, MongoDB Enterprise Advanced, and MongoDB Community Edition.
Tip
X.509 Mechanism
To learn how to use TLS/SSL with the Kotlin Sync driver, see the Enable TLS/SSL on a Connection guide.
For more information about X.509 certificates, see Use x.509 Certificates to Authenticate Clients on Self-Managed Deployments in the MongoDB Server manual.
Specify X.509 Authentication
The examples in this section show how to specify the X.509
authentication mechanism
and use the following placeholder values:
hostname
: The network address of your MongoDB deployment, open to your client.port
: The port number of your MongoDB server.authenticationDb
: The MongoDB database that contains your user's authentication data. If you omit this parameter, the driver uses the default valueadmin
.
Select the Connection String or the MongoCredential tab below for instructions and sample code for specifying this authentication mechanism:
To specify the X.509
authentication mechanism by using a connection
string, set the authMechanism
parameter to MONGODB-X509
and the tls
parameter to true
as shown in the following example:
val mongoClient = MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true")
To specify the X.509
authentication mechanism by using the
MongoCredential
class, use the
createMongoX509Credential()
and applyToSslSettings()
builder methods as shown in the following example:
val credential = MongoCredential.createMongoX509Credential() val settings = MongoClientSettings.builder() .applyToClusterSettings { builder -> builder.hosts(listOf( ServerAddress("<hostname>", <port>)) ) } .applyToSslSettings { builder -> builder.enabled(true) } .credential(credential) .build() val mongoClient = MongoClient.create(settings)
Additional Information
To learn more about authenticating to MongoDB, see Authentication in the MongoDB Server manual.
To learn more about creating a MongoClient
object by using the
Kotlin Sync driver, see the Create a MongoClient guide.