Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /

SCRAM

Salted 挑战响应身份验证机制 (SCRAM) 是一系列身份验证机制,使用用户名和密码对服务器进行身份验证。

Rust驾驶员支持以下基于SCRAM 的身份验证机制:

  • SCRAM-SHA-256 :一种身份验证机制,使用数据库用户名和密码,并通过SHA-256 算法加密。这是默认的身份验证机制。

  • SCRAM-SHA-1:一种身份验证机制,使用数据库用户名和密码,并通过 SHA-1算法加密。

您可以使用SCRAM对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。

提示

SCRAM 机制

要学习;了解有关SCRAM系列身份验证机制的更多信息,请参阅 维基百科上的 RFC5802 Salted 挑战响应身份验证机制。

有关SCRAM的MongoDB实施的更多信息,请参阅MongoDB Server手册中的SCRAM 。

本页上的代码示例使用以下占位符:

  • db_username:您的数据库用户名

  • db_password:您的数据库密码

  • db:与用户关联的身份验证数据库

由于 SCRAM-SHA-256 是默认身份验证机制,因此在实例化 Credential 结构以使用此机制时,可以省略 mechanism字段。

以下示例指定了默认的身份验证机制:

let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).await?;
let default_cred = Credential::builder()
.username("<db_username>".to_string())
.password("<db_password>".to_string())
.source("<db>".to_string())
.build();
client_options.credential = Some(default_cred);
let client = Client::with_options(client_options)?;

要显式指定 SCRAM-SHA-256身份验证机制,请将 Credential 结构的 mechanism字段设立为 AuthMechanism::ScramSha256

let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).await?;
let scram_sha_256_cred = Credential::builder()
.username("<db_username>".to_string())
.password("<db_password>".to_string())
.mechanism(AuthMechanism::ScramSha256)
.source("<db>".to_string())
.build();
client_options.credential = Some(scram_sha_256_cred);
let client = Client::with_options(client_options)?;

要指定 SCRAM-SHA-1身份验证机制,请将 Credential 结构的 mechanism字段设立为 AuthMechanism::ScramSha1

let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).await?;
let scram_sha_1_cred = Credential::builder()
.username("<db_username>".to_string())
.password("<db_password>".to_string())
.mechanism(AuthMechanism::ScramSha1)
.source("<db>".to_string())
.build();
client_options.credential = Some(scram_sha_1_cred);
let client = Client::with_options(client_options)?;

要了解有关向MongoDB进行身份验证的更多信息,请参阅MongoDB Server手册中的身份验证。

要了解有关管理MongoDB部署用户的更多信息,请参阅MongoDB Server手册中的用户。

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档:

后退

身份验证

在此页面上