Overview
通用安全服务API (GSSAPI)身份验证机制允许使用主体名称对Kerberos服务进行身份验证。 只有在向MongoDB Enterprise Advanced进行身份验证时才能使用此机制。
指定Kerberos (GSSAPI) 身份验证
注意
Node.js 驱动程序在 UNIX 上使用 MIT Kerberos 库支持 Kerberos,在 Windows 上使用 SSPI API 支持 Kerberos。
GSSAPI身份验证机制使用您的用户主体对 Kerberos 服务进行身份验证。
在连接string上指定选项时,可以通过执行以下操作来指定此身份验证机制:
将
authMechanism参数设置为GSSAPI。如果使用
mongodb以外的值,请在authMechanismProperties参数中设置SERVICE_NAME值。如果需要自定义服务域,请在
authMechanismProperties参数中指定SERVICE_REALM值。如果需要对主机名进行规范化,请在
authMechanismProperties参数中指定CANONICALIZE_HOST_NAME值。此属性接受以下值:none:(默认)不执行主机名规范化forward:执行正向 DNS 查找以规范化主机名forwardAndReverse:执行正向 DNS 查找,然后对该值执行反向查找以规范化主机名
重要
gssapiServiceName 参数已弃用,可能会在驱动程序的未来版本中删除。请在连接 URI 中使用 authMechanismProperties=SERVICE_NAME:<your service name>。要学习有关连接字符串身份验证选项的更多信息,请参阅服务器手册中连接字符串选项参考的身份验证选项部分。
以下代码示例使用 GSSAPI 对Kerberos for UNIX 进行身份验证。
重要
始终使用encodeURIComponent方法对主体进行URI 编码,以确保对其进行正确解析。
const { MongoClient } = require("mongodb");
// Replace the placeholder values with the values for your environment in the following linesconst clusterUrl = "<cluster_url>";const principal = encodeURIComponent("<Kerberos principal and realm>");const serviceRealm = "<Kerberos service realm>";const canonicalizationSetting = "<canonicalization setting>";const authMechanismProperties = `SERVICE_REALM:${serviceRealm},CANONICALIZE_HOST_NAME:${canonicalizationSetting}`;
const authMechanism = "GSSAPI";
// Connection URIconst uri = `mongodb+srv://${principal}@${clusterUrl}/?authMechanism=${authMechanism}&authMechanismProperties=${authMechanismProperties}`;
const client = new MongoClient(uri);
// Function to connect to the serverasync function run() { try { // Establish and verify connection await client.db("admin").command({ ping: 1 }); console.log("Connected successfully to server"); } finally { // Ensures that the client will close when you finish/error await client.close(); }}run().catch(console.dir);注意
该方法引用 GSSAPI身份验证机制而不是 Kerberos,因为驱动程序通过 GSSAPI RFC-4652(即 SASL 机制)进行身份验证。
API 文档
要学习;了解有关本页讨论的任何方法或类型的更多信息,请参阅以下API文档: