> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON Web Token 认证

> From v3.5.0 onwards, the database supports authenticating client access using JSON Web Token (JWT) for secure token-based authentication.

本主题描述如何在 数据库 中启用 JSON Web Token 认证。

从 v3.5.0 开始，数据库 支持使用 JSON Web Token 进行客户端访问认证。

JSON Web Token（JWT）是一项开放标准（RFC 7519），它定义了一种紧凑、自足的方式，可在各方之间以 JSON 对象的形式安全地传输信息。由于该信息经过数字签名，因此可以验证和信任。JWT 可以使用密文（HMAC 算法）或使用 RSA 或 ECDSA 的公钥/私钥对进行签名。

本主题描述如何在 数据库 中使用 JWT 认证手动创建和认证用户。有关如何通过安全集成将 数据库 与 JWT 认证集成的说明，请参见 [通过安全集成认证用户](/documents/cn/4.2/administration/user_privs/authentication/security_integration.md)。有关如何在使用 JWT 认证用户组的更多信息，请参见 [认证用户组](/documents/cn/4.2/administration/user_privs/group_provider.md)。

## 前提条件

如果您想从 MySQL 客户端连接到 数据库，MySQL 客户端版本必须是 9.1 或更高版本。从 9.1 起，所有发行版（包括社区版）的 MySQL 客户端都自带 `authentication_openid_connect_client` 插件。

## 使用 JWT 认证创建用户

创建用户时，通过 `IDENTIFIED WITH authentication_jwt [AS '{xxx}']` 指定认证方法为 JWT。`{xxx}` 是用户的 JWT 属性。除了当前方式，您还可以在 FE 配置文件中配置默认的 JWT 属性。您需要手动修改所有 **fe.conf** 文件并重启所有 FE 使之生效。配置默认属性后，当您省略 `AS '{xxx}'` 部分时，数据库 会使用您配置文件中指定的默认属性。

语法：

```SQL theme={null}
CREATE USER <username> IDENTIFIED WITH authentication_jwt [AS 
'{
  "jwks_url": "<jwks_url>",
  "principal_field": "<principal_field>",
  "required_issuer": "<required_issuer>",
  "required_audience": "<required_audience>"
}']
```

| 属性名 | 对应 FE 配置项 | 描述 |
| - | - | - |
| `jwks_url` | `jwt_jwks_url` | JSON Web Key Set (JWKS) 服务的 URL 或 `fe/conf` 目录下公钥本地文件的路径。 |
| `principal_field` | `jwt_principal_field` | 用于标识 JWT 中主体 (`sub`) 的字段的字符串。默认值为 `sub`。此字段的值必须与登录 数据库 的用户名相同。 |
| `required_issuer` | `jwt_required_issuer` | (可选) 用于标识 JWT 中发行者 (`iss`) 的字符串列表。仅当列表中的某个值与 JWT 发行者匹配时，JWT 才被视为有效。 |
| `required_audience` | `jwt_required_audience` | (可选) 用于标识 JWT 中受众 (`aud`) 的字符串列表。仅当列表中的某个值与 JWT 受众匹配时，JWT 才被视为有效。 |

示例：

```SQL theme={null}
CREATE USER tom IDENTIFIED WITH authentication_jwt AS
'{
  "jwks_url": "http://localhost:38080/realms/master/protocol/jwt/certs",
  "principal_field": "preferred_username",
  "required_issuer": "http://localhost:38080/realms/master",
  "required_audience": "starrocks"
}';
```

如您已经使用 FE 配置项进行配置，则可以直接使用以下语句：

```SQL theme={null}
CREATE USER tom IDENTIFIED WITH authentication_jwt;
```

## 使用 JWT 从 MySQL 客户端连接

要使用 JWT 从 MySQL 客户端连接到 数据库，您需要启用 `authentication_openid-connect_client` 插件，并传递必要的令牌（使用令牌文件的路径）来认证映射的用户。

语法：

```Bash theme={null}
mysql -h <hostname> -P <query_port> --authentication-openid-connect-client-id-token-file=<path_to_token_file> -u <username>
```

示例：

```Bash theme={null}
mysql -h 127.0.0.1 -P 9030 --authentication-openid-connect-client-id-token-file=/path/to/token/file -u tom
```

## 使用 JWT 通过 HTTP 或 Arrow Flight 连接

JWT 安全集成的用户还可以把 token 放在 `Authorization: Bearer <token>` 请求头中访问 HTTP API（SQL API、Stream Load、Web UI）和 Arrow Flight SQL，无需提供用户名。参见[通过 HTTP 和 Arrow Flight 连接](/documents/cn/4.2/administration/user_privs/authentication/security_integration.md#通过-http-和-arrow-flight-连接)。

> 用 `IDENTIFIED WITH authentication_jwt` 创建的用户只能在 MySQL 协议上出示 token，因为那里的握手包带用户名。要在 HTTP API 或 Arrow Flight SQL 上使用 JWT，请改为创建 JWT [安全集成](/documents/cn/4.2/administration/user_privs/authentication/security_integration.md)，并参见[各认证方式在哪些入口可用](/documents/cn/4.2/administration/user_privs/authentication/authentication.md#各认证方式在哪些入口可用)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.