You can add a database user to MongoDB using Workforce authentication. This process allows your organization’s identity provider to manage user access, ensuring secure and centralized authentication for database operations.您可以使用Workforce身份验证将数据库用户添加到MongoDB。此过程允许您组织的身份提供者管理用户访问,确保数据库操作的安全和集中身份验证。
Before you Begin开始之前
You must Configure an External Identity Provider for Workforce Authentication.您必须为劳动力身份验证配置外部身份提供程序。You must Configure MongoDB with Workforce Identity Federation.您必须使用劳动力身份联合配置MongoDB。
Note
Your oidcIdentityProviders configuration determines the approach you must take to authorize users:oidcIdentityProviders配置决定了您必须采取的授权用户的方法:
If the如果useAuthorizationClaimfield is set tofalseto enable internal authorization, authorize users with user IDs.useAuthorizationClaim字段设置为false以启用内部授权,请使用用户ID授权用户。If the field is set to如果该字段设置为true, authorize users with identity provider groups.true,则使用身份提供程序组授权用户。
Steps步骤
Authorize Users with IdP Groups使用IdP组授权用户
Create MongoDB roles创建MongoDB角色
In the 在admin database, use the db.createRole() method to create roles that map the identity provider group roles to MongoDB roles.admin数据库中,使用db.createRole()方法创建将身份提供者组角色映射到MongoDB角色的角色。
Use the following format to create roles:使用以下格式创建角色:
<authNamePrefix>/<authorizationClaim>
The oidcIdentityProviders parameter provides the authNamePrefix field and the authorizationClaim field. For example:oidcIdentityProviders参数提供authNamePrefix字段和authorizationClaim字段。例如:
db.createRole( {
role: "okta/Everyone",
privileges: [ ],
roles: [ "readWriteAnyDatabase" ]
} )Authorize Users with User IDs使用用户ID授权用户
Switch to the $external database切换到$external数据库
When you create a user, you must create it in the 创建用户时,必须在$external database. To switch to the $external database, run the following command:$external数据库中创建它。要切换到$external数据库,请运行以下命令:
use $externalCreate a user创建一个用户
To create users and add them to your MongoDB database, use the 要创建用户并将其添加到MongoDB数据库中,请使用db.createUser() command.db.createUser()命令。
Use the following format for the user field, where the authNamePrefix and authorizationClaim values come from the oidcIdentityProviders parameter:user字段使用以下格式,其中authNamePrefix和authorizationClaim值来自oidcIdentityProviders参数:
<authNamePrefix>/<authorizationClaim>
To create a user in MongoDB with the 要在MongoDB中创建authNamePrefix of okta and an authorizationClaim of jane.doe, run the following:authNamePrefix为okta、authorizationClaim为jane.doe的用户,请运行以下命令:
db.createUser( {
user: "okta/jane.doe",
roles: [
{
role: "readWriteAnyDatabase",
db: "admin"
}
]
} )Next Steps后续步骤
You can connect an application to MongoDB using Workforce Identity Federation in the following ways:您可以通过以下方式使用Workforce Identity Federation将应用程序连接到MongoDB:
- Compass
-
For more details on MongoDB Shell OIDC options, see Authentication Options有关MongoDB Shell OIDC选项的更多详细信息,请参阅身份验证选项