用户与权限 REST API
🌐 Users & Permissions REST API
Page summary:
“用户与权限”功能提供用于身份验证、用户管理、角色和权限的 REST API 端点。
“用户与权限”功能提供了一组用于身份验证、用户管理和角色/权限管理的 REST API 端点。这些端点与标准的内容类型 CRUD 端点是分开的,并且具有自己的响应格式。有关概览和配置选项,请参阅 用户与权限介绍。
🌐 The Users & Permissions feature exposes a set of REST API endpoints for authentication, user management, and role/permission management. These endpoints are separate from the standard content-type CRUD endpoints and have their own response shapes. For a general overview and configuration options, see the Users & Permissions introduction.
所有端点都使用 /api 前缀。例如,如果你的 Strapi 服务器运行在 http://localhost:1337,登录端点是 http://localhost:1337/api/auth/local。
🌐 All endpoints use the /api prefix. For example, if your Strapi server runs at http://localhost:1337, the login endpoint is http://localhost:1337/api/auth/local.
身份验证
🌐 Authentication
身份验证端点处理登录、注册和密码管理。这些端点大多数默认是公开的,不需要 Bearer 令牌。
🌐 Authentication endpoints handle login, registration, and password management. Most of these endpoints are public by default and do not require a Bearer token.
登录
🌐 Login
POST /api/auth/local
使用用户的标识符(电子邮件或用户名)和密码进行身份验证,返回 JWT 和用户对象。
🌐 Authenticates a user with their identifier (email or username) and password, returning a JWT and the user object.
curl -X POST http://localhost:1337/api/auth/local \
-H "Content-Type: application/json" \
-d '{"identifier": "user@example.com", "password": "yourPassword"}'
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"documentId": "x74detpqybxw0bn6ormua5g2",
"username": "testuser1",
"email": "user@example.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"createdAt": "2024-03-15T10:00:00.000Z",
"updatedAt": "2024-03-15T10:00:00.000Z",
"publishedAt": "2024-03-15T10:00:00.000Z"
}
}
可能的错误:
🌐 Possible errors:
| 状态 | 信息 | 原因 |
|---|---|---|
| 400 | "Invalid identifier or password" | 凭证错误 |
| 400 | "Your account email is not confirmed" | 需要电子邮件确认但未完成 |
| 400 | "Your account has been blocked by an administrator" | 账户被封禁 |
此端点有限流(默认:每5分钟5次请求)。
🌐 This endpoint is rate limited (default: 5 requests per 5 minutes).
注册
🌐 Register
POST /api/auth/local/register
创建一个新的用户账户,并返回一个 JWT 以及用户对象。
🌐 Creates a new user account and returns a JWT along with the user object.
curl -X POST http://localhost:1337/api/auth/local/register \
-H "Content-Type: application/json" \
-d '{"username": "newuser", "email": "newuser@example.com", "password": "Password123!"}'
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 2,
"documentId": "xjpaytstw1gm7wdfoc6c2k13",
"username": "newuser",
"email": "newuser@example.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"createdAt": "2024-03-15T10:00:00.000Z",
"updatedAt": "2024-03-15T10:00:00.000Z",
"publishedAt": "2024-03-15T10:00:00.000Z"
}
}
默认情况下,请求体中只接受 username、email 和 password。要允许额外的字段(例如,你在用户内容类型中添加的 fullName 字段),你必须在 register.allowedFields 配置中明确列出它们。详情请参见 注册配置。
🌐 Only username, email, and password are accepted in the request body by default. To allow additional fields (e.g., a fullName field you added to the User content-type), you must explicitly list them in the register.allowedFields configuration. See Registration configuration for details.
启用电子邮件确认时,响应仅包含用户对象,而不包含 JWT。用户必须确认他们的电子邮件后才能登录。
🌐 When email confirmation is enabled, the response contains only the user object without a JWT. The user must confirm their email before they can log in.
新注册的用户会被分配默认角色,除非在 设置 > 用户与权限 > 高级设置 中更改,否则默认角色为“已认证”.
🌐 The newly registered user is assigned the default role, which is "Authenticated" unless changed in Settings > Users & Permissions > Advanced Settings.
可能的错误:
🌐 Possible errors:
| 状态 | 信息 | 原因 |
|---|---|---|
| 400 | "Email or Username are already taken" | 邮箱或用户名重复 |
| 400 | "Invalid parameters: fieldName" | allowedFields 中未列出的额外字段 |
| 400 | "Register action is currently disabled" | 管理设置中禁用了注册 |
此端点有速率限制。
🌐 This endpoint is rate limited.
忘记密码
🌐 Forgot password
POST /api/auth/forgot-password
向指定的地址发送密码重置电子邮件。需要配置电子邮件提供商。
🌐 Sends a password reset email to the specified address. Requires an email provider to be configured.
curl -X POST http://localhost:1337/api/auth/forgot-password \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com"}'
{
"ok": true
}
这个端点总是返回 { "ok": true },无论电子邮件地址是否存在于系统中。这是有意为之,以防止用户枚举攻击。
🌐 This endpoint always returns { "ok": true } regardless of whether the email address exists in the system. This is intentional to prevent user enumeration attacks.
此端点有速率限制。
🌐 This endpoint is rate limited.
重置密码
🌐 Reset password
POST /api/auth/reset-password
使用通过电子邮件收到的令牌重置用户的密码。code、password 和 passwordConfirmation 字段都是必填的。
🌐 Resets a user's password using a token received by email. The code, password, and passwordConfirmation fields are all required.
curl -X POST http://localhost:1337/api/auth/reset-password \
-H "Content-Type: application/json" \
-d '{"code": "resetTokenFromEmail", "password": "NewPassword123!", "passwordConfirmation": "NewPassword123!"}'
{
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"documentId": "x74detpqybxw0bn6ormua5g2",
"username": "testuser1",
"email": "user@example.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"createdAt": "2024-03-15T10:00:00.000Z",
"updatedAt": "2024-03-15T10:00:00.000Z",
"publishedAt": "2024-03-15T10:00:00.000Z"
}
}
可能的错误:
🌐 Possible errors:
| 状态 | 消息 | 原因 |
|---|---|---|
| 400 | "passwordConfirmation is a required field" | 缺少 passwordConfirmation |
| 400 | "Passwords do not match" | password 和 passwordConfirmation 不一致 |
| 400 | "Incorrect code provided" | 重置令牌无效或已过期 |
此端点有速率限制。
🌐 This endpoint is rate limited.