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

Kerberos (GSSAPI) 身份验证机制

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

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

  • kerberos_principal: Your Kerberos principal. 示例用户名是myuser@KERBEROS.EXAMPLE.COM

  • password:Kerberos 用户的密码。 您还可以将密码存储在keytab文件中,避免在代码中暴露密码。

  • connection_uri:您的连接字符串 URI。

You must use the gssapi build tag and specify cgo support during compilation to use Kerberos authentication. cgo support is enabled by default unless you previously set environment variables to cross-compile to a different platform. To use the gssapi build tag, compile your code with the following command:

go build -tags gssapi

以下代码演示如何定义 Credential 结构以向 Kerberos 进行身份验证,以及如何使用您的身份验证首选项创建客户端:

credential := options.Credential{
AuthMechanism: "GSSAPI",
Username: "<kerberos_principal>",
Password: "<password>",
PasswordSet: true,
}
uri := "<connection_uri>"
clientOpts := options.Client().ApplyURI(uri).SetAuth(credential)
client, err := mongo.Connect(clientOpts)

如果将身份验证密钥存储在 keytab 文件中,则可以省略密码或 Credential 结构中的 PasswordSet字段。您可以使用 kinit 二进制文件初始化档案缓存,用于对Kerberos主体进行身份验证。要学习;了解有关 kinit 二进制文件的更多信息,请参阅Oracle文档。

以下命令显示如何为示例用户名调用凭证缓存:

kinit myuser@KERBEROS.EXAMPLE.COM

You can alternatively authenticate using a connection string URI, specifying your URL-encoded Kerberos principal, password, and hostname, the network address of your MongoDB server:

uri := "mongodb://<kerberos_principal>:<password>@<hostname>/?authMechanism=GSSAPI"

您可以使用 Credential 结构中的 AuthMechanismProperties字段通过身份验证机制指定更多属性。Kerberos的默认服务名称是“ MongoDB”。 以下代码展示了如何在定义 Credential 结构体时为 SERVICE_NAMESERVICE_REALM 字段设立自定义值:

credential := options.Credential{
AuthMechanism: "GSSAPI",
Username: "<kerberos_principal>",
Password: "<password>",
AuthMechanismProperties: map[string]string{
"SERVICE_REALM": "<kerberos_service_realm>",
"SERVICE_NAME": "<service_name>",
},
}

有关更多属性,请参阅服务器手册中有关身份验证属性的条目。

要学习;了解有关本页讨论的任何方法或类型的更多信息,请参阅以下API文档: