对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Kerberos (GSSAPI) 身份验证机制

通用安全服务API (GSSAPI)身份验证机制允许使用主体名称对Kerberos服务进行身份验证。 只有在向MongoDB Enterprise Advanced进行身份验证时才能使用此机制。

注意

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 lines
const 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 URI
const uri = `mongodb+srv://${principal}@${clusterUrl}/?authMechanism=${authMechanism}&authMechanismProperties=${authMechanismProperties}`;
const client = new MongoClient(uri);
// Function to connect to the server
async 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文档: