Overview
Salted 挑战响应身份验证机制 (SCRAM ) 是MongoDB的默认身份验证机制。 Scala驾驶员支持以下SCRAM变体:
SCRAM-SHA-256:使用 SHA-256 哈希算法,并且是MongoDB 6.0 及更高版本的默认机制。SCRAM-SHA-1:使用 SHA-1 哈希算法。
您可以使用SCRAM对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。
提示
SCRAM 机制
代码占位符
本页上的代码示例使用以下占位符:
<db_username>:要进行身份验证的用户的MongoDB用户名。<db_password>:要进行身份验证的用户的MongoDB密码。<hostname>: MongoDB 部署的网络解决。<port>: MongoDB 部署的端口号。如果省略此参数,驾驶员将使用默认端口号 (27017)。连接到MongoDB Atlas 群集时,您不需要端口号。<authenticationDb>:包含用户身份验证数据的 MongoDB 数据库。如果省略此参数,驱动程序将使用默认值admin。
要使用代码示例,请将这些占位符替换为您自己的值。
默认身份验证机制
MongoDB 6.0 及更高版本的默认身份验证机制是 SCRAM-SHA-256。要使用默认机制,请选择以下标签页之一:
要使用连接字符串进行身份验证,请省略 authMechanism 参数:
val mongoClient = MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>")
MongoCredential 类表示身份验证档案,并包括每种身份验证机制的静态工厂方法。要使用 MongoCredential 类进行身份验证,请使用 createCredential() 方法:
val user = "<db_username>" // the username val source = "<authenticationDb>" // the authentication database val password = ... // the password as a character array val credential = MongoCredential.createCredential(user, source, password) val mongoClient = MongoClient(MongoClientSettings .builder() .applyToClusterSettings(builder => builder.hosts(Collections.singletonList(ServerAddress("<hostname>", <port>)))) .credential(credential) .build())
SCRAM-SHA-256
要使用此机制,featureCompatibilityVersion 必须为 4.0 或更高版本。要指定 SCRAM-SHA-256身份验证机制,请选择以下标签页之一:
要使用连接字符串进行身份验证,请将 authMechanism 参数设立为 SCRAM-SHA-256:
val mongoClient = MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-256")
MongoCredential 类表示身份验证档案,并包括每种身份验证机制的静态工厂方法。要使用 MongoCredential 类进行身份验证,请使用 createScramSha256Credential() 方法:
val user = "<db_username>" // the username val source = "<authenticationDb>" // the authentication database val password = ... // the password as a character array val credential = MongoCredential.createScramSha256Credential(user, source, password) val mongoClient = MongoClient(MongoClientSettings .builder() .applyToClusterSettings(builder => builder.hosts(Collections.singletonList(ServerAddress("<hostname>", <port>)))) .credential(credential) .build())
SCRAM-SHA-1
要指定 SCRAM-SHA-1身份验证机制,请选择以下标签页之一:
要使用连接字符串进行身份验证,请将 authMechanism 参数设立为 SCRAM-SHA-1:
val mongoClient = MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-1")
MongoCredential 类表示身份验证档案,并包括每种身份验证机制的静态工厂方法。要使用 MongoCredential 类进行身份验证,请使用 createScramSha1Credential() 方法:
val user = "<db_username>" // the username val source = "<authenticationDb>" // the authentication database val password = ... // the password as a character array val credential = MongoCredential.createScramSha1Credential(user, source, password) val mongoClient = MongoClient(MongoClientSettings .builder() .applyToClusterSettings(builder => builder.hosts(Collections.singletonList(ServerAddress("<hostname>", <port>)))) .credential(credential) .build())
API 文档
有关此页面上的方法和类型的更多信息,请参阅以下API文档: