Docs Menu
Docs Home
/ / /
Java Reactive Streams ドライバー
/

エンタープライズ認証メカニズム

項目一覧

  • Overview
  • 認証メカニズムを指定する
  • メカニズム
  • Kerberos (GSSAPI)
  • LDAP (PLAIN)
  • MONGODB-OIDC

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.

MongoDB に接続するときに、次のいずれかの方法を使用して認証メカニズムと認証情報を指定できます。

  • 接続文字列

  • MongoCredential factory method

接続string接続 URIとも呼ばれます)は、 MongoDBクラスターへの接続と認証の方法を指定します。

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.

ジェネリック セキュリティ サービス API(GSSAPI)認証メカニズムを使用すると、プリンシパル名を使用して Kerberos サービスで認証できます。

次のセクションには、次のプレースホルダーを使用するコード例が含まれています。

  • username: URL エンコードされたプリンシパル名(例: "username%40REALM.ME"

  • hostname: クライアントがアクセスできる MongoDB 配置のネットワーク アドレス

  • port: MongoDB 配置のポート番号

Connection StringMongoCredential対応する構文を表示するには、 タブまたは タブを選択します。

次の例では、接続文字列を使用して GSSAPI を認証します。

MongoClient mongoClient = MongoClients
.create("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI");

MongoCredentialクラスを使用して GSSAPI 認証メカニズムを指定するには、次の例に示すように、 createGSSAPICredential()メソッドを呼び出します。

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

Kerberos の設定によっては、次の追加のMongoCredentialメカニズム プロパティを 1 つ以上指定する必要がある場合があります。

  • SERVICE_NAME

  • CANONICALIZE_HOST_NAME

  • JAVA_SUBJECT

  • JAVA_SASL_CLIENT_PROPERTIES

  • JAVA_SUBJECT_PROVIDER

重要

次の GSSAPI プロパティは、 MongoCredentialクラスでのみ指定できます。

  • JAVA_SUBJECT

  • JAVA_SASL_CLIENT_PROPERTIES

  • JAVA_SUBJECT_PROVIDER

これらのプロパティを指定する方法については、 MongoCredentialタブを選択してください。

GSSAPI 追加プロパティを指定するには、接続文字列にプロパティを<PROPERTY_NAME>:<value>形式の URL パラメータとして含めます。

次の例では、GSSAPI で認証し、追加のプロパティを指定しています。

MongoClient mongoClient = MongoClients
.create("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService");

GSSAPI 追加プロパティを指定するには、 MongoCredentialインスタンスでwithMechanismProperty()メソッドを呼び出し、プロパティ名と値をパラメーターとして渡します。 MongoCredentialクラスで定義されているプロパティ名定数を使用します。

対応するプロパティを指定する方法を確認するには、 SERVICE_NAME_KEYタブまたはJAVA_SUBJECT_KEYタブを選択します。

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);

デフォルトでは、Java Reactive Streams ドライバーはMongoClientインスタンスごとに Kerberos チケットをキャッシュします。配置でMongoClientインスタンスが頻繁に作成され、破棄される場合は、デフォルトの Kerberos チケット キャッシュ動作を プロセスごとにキャッシュすることでパフォーマンスを向上させることができます。

プロセスごとに Kerberos チケットをキャッシュするには、 MongoCredential認証メカニズムを使用する必要があります。接続文字列認証メカニズムではJAVA_SUBJECT_PROVIDERメカニズムのプロパティがサポートされていないためです。プロセス別に Kerberos チケットをキャッシュする方法については、 MongoCredentialタブを選択してください。

プロセスごとに Kerberos チケットをキャッシュするには、JAVA_SUBJECT_PROVIDER メカニズム プロパティを指定し、 KerberosSubjectProvider を提供しますMongoCredential 次の例に示すように、 インスタンスの場合、

/* 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));

注意

Windows では、Oracle の JRE は LSA を使用しています では なく これは Windows Active Directory およびシングル サインオンの実装との相互運用性を制限します。詳細については、次のリソースを参照してください。

ディレクトリ サーバーのユーザー名とパスワードを使用して、LDAP(Lightweight Directory Access Protocol)サーバーで認証できます。

Tip

認証メカニズムは 、RFC-4616 で定義されている PLAIN 簡易認証とセキュリティ層(SASL) を使用して認証するため、 ではなく という名前が付けられていますPLAIN LDAP

次のセクションには、次のプレースホルダーを使用するコード例が含まれています。

  • ldap_username: LDAP ユーザー名

  • ldap_password: LDAP ユーザーのパスワード

  • hostname: クライアントがアクセスできる MongoDB 配置のネットワーク アドレス

  • port: MongoDB 配置のポート番号

Connection StringMongoCredential対応する構文を表示するには、 タブまたは タブを選択します。

MongoClient mongoClient = MongoClients
.create("<ldap_username>:<ldap_password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN");

MongoCredentialクラスを使用して LDAP(PLAIN)認証メカニズムを指定するには、次の例に示すように、 createPlainCredential()メソッドを呼び出します。

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());

重要

MONGODB-OIDC 認証メカニズムでは、Linux プラットフォーム上で MongoDB Server v 7.0以降を実行する必要があります。

次のセクションでは、MONGODB-OIDC 認証メカニズムを使用してさまざまなプラットフォームに認証する方法について説明します。

MONGODB-OIDC 認証メカニズムの詳細については、 MongoDB ServerMongoDB Serverマニュアルの 「 OpenID Connect 認証 」 と 「 パラメーター 」 を参照してください。

アプリケーションが Azure VM 上で実行され、 や Azure インスタンス メタデータ サービス が使用される場合 (MDS)では、Java Reactive Streams ドライバーの組み込み Azure サポートを使用して MongoDB で認証できます。

Azure IMDS OIDC 認証は、MongoCredential を使用するか、 接続stringの一部として指定できます。

Connection StringMongoCredential対応する構文を表示するには、 タブまたは タブを選択します。

<username>プレースホルダーを、Azure が管理する IdP またはエンタープライズ アプリケーションのクライアント ID またはアプリケーション ID に置き換えます。次のコード内の<percent-encoded audience>プレースホルダーを、MongoDB 配置に構成されたオーディエンス サーバー パラメータのパーセント エンコードされた値に置き換えます。

カンマ( , )文字とそのエンコーディング( %2C )は予約されており、これらの文字を値で使用すると、ドライバーはカンマをキーと値のペアの区切り文字として解釈します。 MongoCredentialタブに示されているように、 MongoCredentialインスタンスではカンマを含む値を指定する必要があります。

MongoClient mongoClient = MongoClients.create(
"mongodb://<username>@<hostname>:<port>/?" +
"?authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>");

<username>プレースホルダーを、Azure が管理する IdP またはエンタープライズ アプリケーションのクライアント ID またはアプリケーション ID に置き換えます。 <audience>プレースホルダーを、MongoDB 配置に構成されたaudienceサーバー パラメータの値に置き換えます。

MongoCredential credential = MongoCredential.createOidcCredential("<username>")
.withMechanismProperty("ENVIRONMENT", "azure")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>");
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
.credential(credential)
.build());

アプリケーションが Google Compute Engine VM で実行され、または GCP インスタンス メタデータ サービス を使用している場合 、Java Reactive Streams ドライバーに組み込まれている GCP サポートを使用して、MongoDB に認証できます。

GCP IMDS OIDC 認証は、MongoCredential を使用するか、接続stringの一部として指定できます。

次のセクションには、次のプレースホルダーを使用するコード例が含まれています。

  • hostname: クライアントがアクセスできる MongoDB 配置のネットワーク アドレス

  • port: MongoDB 配置のポート番号

Connection StringMongoCredential対応する構文を表示するには、 タブまたは タブを選択します。

次のコード内の<percent-encoded audience>プレースホルダーを、MongoDB 配置に構成されたオーディエンス サーバー パラメータのパーセント エンコードされた値に置き換えます。

カンマ( , )文字とそのエンコーディング( %2C )は予約されており、これらの文字を値で使用すると、ドライバーはカンマをキーと値のペアの区切り文字として解釈します。 MongoCredentialタブに示されているように、 MongoCredentialインスタンスではカンマを含む値を指定する必要があります。

MongoClient mongoClient = MongoClients.create(
"mongodb://<hostname>:<port>/?" +
"authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>");

<audience>プレースホルダーを、MongoDB 配置に構成されたaudienceサーバー パラメータの値に置き換えます。

MongoCredential credential = MongoCredential.createOidcCredential()
.withMechanismProperty("ENVIRONMENT", "gcp")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>");
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
.credential(credential)
.build());

Java Reactive Streams ドライバーでは、Azure Functions や Azure Kubernetes Service(AKS)を含むすべてのプラットフォームで組み込みサポートを提供していません。代わりに、OIDC を使用してこれらのプラットフォームから認証するためのカスタム コールバックを定義する必要があります。そのためには、次のコード例に示すように、 "OIDC_CALLBACK"認証プロパティを使用します。

MongoCredential credential = MongoCredential.createOidcCredential(null)
.withMechanismProperty("OIDC_CALLBACK", (context) -> {
String accessToken = ...
return new OidcCallbackResult(accessToken);
});

"OIDC_CALLBACK"プロパティの値は、 OidcCallbackContextをパラメータとして受け入れ、 OidcCallbackResultを返すOidcCallback関数インターフェースの Lambda またはその他の実装である必要があります。

次の例では、サンプル コールバックを使用して、ローカル ファイル システム内の"access-token.dat"という名前のファイルから OIDC トークンを検索します。

MongoCredential credential = MongoCredential.createOidcCredential(null)
.withMechanismProperty("OIDC_CALLBACK", (context) -> {
string accessToken = new String(Files.readAllBytes(Paths.get("access-token.dat"));
return new OidcCallbackResult(accessToken);
});
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
.credential(credential)
.build());

戻る

認証