Overview
Salted Challenge Response Authentication Mechanism(SCRAM)は、チャレンジ レスポンス メカニズムを使用してユーザーを認証する認証メカニズムのファミリーです。256 RFC7677 で定義されている SCRAM-SHA- は、SHA-256 アルゴリズムを使用してパスワードをハッシュし、 MongoDB v ..4 0を実行中配置のデフォルトの認証メカニズムです。以降に更新します。1 RFC5802 で定義されている SCRAM-SHA- は、 MongoDB v. を実行中配置との互換性でサポートされています。36
SCRAM を使用して、 MongoDB Atlas、 MongoDB Enterprise Advanced、 MongoDB Community Edition を認証できます。
Tip
SCRAM メカニズム
SCRAM ファミリーの認証メカニズムの詳細については、 Wikipedia の RFC5802 および Salted Challenge Response Authentication Mechanism を参照してください。
MongoDBの SCRAM実装の詳細については、 MongoDB Serverマニュアルの「 SCRAM 」を参照してください。
コード プレースホルダー
このページのコード例では、次のプレースホルダーを使用します。
<db_username>: 認証するユーザーのMongoDBデータベースユーザー名。<db_password>: 認証するユーザーのMongoDBデータベースパスワード。<hostname>: MongoDBデプロイのネットワーク アドレス。<port>: MongoDBデプロイのポート番号。 このパラメーターを省略すると、ドライバーはデフォルトのポート番号(27017)を使用します。 MongoDB Atlasクラスターに接続する場合、ポート番号は必要ありません。
このページのコード例を使用するには、これらのプレースホルダーを独自の値に置き換えます。
アプリケーションでの SCRAM 認証の使用
SCRAM を使用してMongoDBユーザーを認証するには、接続 URI でMongoDB のユーザー名とパスワード、および authSource パラメーターと authMechanism パラメーターを指定します。このページの例では、デフォルトの認証データベースである authSource=admin を使用し、authMechanism を SCRAM-SHA-256 または SCRAM-SHA-1 に設定します。
次の方法で接続できます。
接続 URI のみを使用する場合
mongocxx::options::clientオブジェクトで設定されたトランスポート層セキュリティ(TLS)オプションで接続 URI を使用する場合
次のタブには、SCRAM-SHA-256 と SCRAM-SHA-1 の両方の TLS 接続と非 TLS 接続の例を示します。使用する認証メカニズムと接続方法に一致するタブを選択します。
auto uri = mongocxx::uri("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "authSource=admin&authMechanism=SCRAM-SHA-256"); auto client = mongocxx::client(uri);
mongocxx::options::client client_options; mongocxx::options::tls tls_options; tls_options.pem_file("path/to/ca-or-client.pem"); client_options.tls_opts(tls_options); auto uri = mongocxx::uri( "mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "authSource=admin&authMechanism=SCRAM-SHA-256&tls=true"); auto client = mongocxx::client(uri, client_options);
auto uri = mongocxx::uri("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "authSource=admin&authMechanism=SCRAM-SHA-1"); auto client = mongocxx::client(uri);
mongocxx::options::client client_options; mongocxx::options::tls tls_options; tls_options.pem_file("path/to/ca-or-client.pem"); client_options.tls_opts(tls_options); auto uri = mongocxx::uri( "mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "authSource=admin&authMechanism=SCRAM-SHA-1&tls=true"); auto client = mongocxx::client(uri, client_options);
API ドキュメント
C++ドライバーでmongocxx::clientオブジェクトを作成する方法の詳細については、次のAPIドキュメントを参照してください。