@@ -428,18 +428,36 @@ Kubernetes
428
428
If your application runs on a Kubernetes cluster, you can authenticate to MongoDB by using
429
429
the {+driver-short+}'s built-in Kubernetes support.
430
430
431
- To specify Kubernetes OIDC as the authentication mechanism, set the following
432
- options in your connection string:
431
+ Select from the :guilabel:`Connection String` or :guilabel:`MongoCredential` tabs to
432
+ see the corresponding syntax.
433
433
434
- - ``authMechanism``: Set to ``MONGODB-OIDC``.
435
- - ``authMechanismProperties``: Set to ``ENVIRONMENT:k8s``.
434
+ .. tabs::
436
435
437
- The following code example shows how to set the preceding connection options:
436
+ .. tab:: Connection String
437
+ :tabid: mongodb-kubernetes-connection-string
438
438
439
- .. code-block:: java
439
+ .. code-block:: java
440
+
441
+ MongoClient mongoClient = MongoClients.create(
442
+ "mongodb://<hostname>:<port>/" +
443
+ "?authMechanism=MONGODB-OIDC" +
444
+ "&authMechanismProperties=ENVIRONMENT:k8s");
445
+
446
+ .. tab:: MongoCredential
447
+ :tabid: mongodb-kubernetes-mongo-credential
448
+
449
+ .. code-block:: java
450
+
451
+ MongoCredential credential = MongoCredential.createOidcCredential()
452
+ .withMechanismProperty("ENVIRONMENT", "k8s");
453
+
454
+ MongoClient mongoClient = MongoClients.create(
455
+ MongoClientSettings.builder()
456
+ .applyToClusterSettings(builder ->
457
+ builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
458
+ .credential(credential)
459
+ .build());
440
460
441
- String uri = "mongodb://<hostname>:<port>/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s";
442
- MongoClient mongoClient = MongoClients.create(uri)
443
461
444
462
Custom Callback
445
463
+++++++++++++++
0 commit comments