文档文档

创建令牌

使用 InfluxDB 用户界面 (UI)、influx 命令行界面 (CLI) 或 InfluxDB API 创建 API 令牌。

为了遵循安全 API 令牌生成和检索的最佳实践,InfluxDB 对 API 令牌强制执行访问限制。

  • 令牌对创建令牌的用户可见。
  • InfluxDB 仅允许在令牌创建后立即访问 API 令牌值。
  • 创建 API 令牌后,您无法更改其访问(读取/写入)权限。
  • 当创建令牌的用户被删除时,令牌将停止工作。

我们建议您按以下方式管理令牌

  • 创建一个通用用户来创建和管理用于写入数据的令牌。
  • 将您的令牌存储在安全的密码库中以供将来访问。

在 InfluxDB UI 中管理令牌

要在 InfluxDB UI 中管理 InfluxDB API 令牌,请导航到 API 令牌 管理页面。

在左侧导航菜单中,选择 数据 (加载数据) > API 令牌

在 InfluxDB UI 中创建令牌

  1. API 令牌管理页面 中,单击 生成 并选择令牌类型(读/写令牌所有访问 API 令牌)。
  2. 在出现的窗口中,在 描述 字段中输入令牌的描述。
  3. 如果生成的是读/写令牌
    • 读取 窗格中搜索并选择要从中读取数据的存储桶。
    • 写入 窗格中搜索并选择要写入数据的存储桶。
  4. 单击 保存

使用 influx CLI 创建令牌

使用 influx auth create 命令 创建令牌。在命令中包含标志以授予令牌特定的权限。请参阅 可用标志。只有具有 write: authorizations 权限的令牌才能创建令牌。

# Syntax
influx auth create -o <org-name> [permission-flags]

示例

创建所有访问令牌

创建所有访问令牌以授予对组织中所有资源的权限。

influx auth create \
  --org my-org \
  --all-access

创建操作员令牌

创建操作员令牌以授予对所有组织中所有资源的权限。

influx auth create \
  --org my-org \
  --operator

要在设置过程完成后,使用 InfluxDB UI、api/v2 API 或 influx CLI 查看或创建操作员令牌,您必须使用现有的操作员令牌。

要创建新的操作员令牌而不使用现有令牌,请参阅如何使用 influxd recovery auth CLI。

创建具有指定权限的令牌

创建具有指定读取权限的令牌
influx auth create \
  --org my-org \
  --read-bucket 03a2bbf46309a000 \
  --read-bucket 3a87c03ace269000 \
  --read-dashboards \
  --read-tasks \
  --read-telegrafs \
  --read-user
创建作用域限定为用户且具有指定读取和写入权限的令牌
influx auth create       \
  --org ORG_NAME         \
  --user USERNAME        \
  --read-authorizations  \
  --write-authorizations \
  --read-buckets         \
  --write-buckets        \
  --read-dashboards      \
  --write-dashboards     \
  --read-tasks           \
  --write-tasks          \
  --read-telegrafs       \
  --write-telegrafs      \
  --read-users           \
  --write-users

有关其他可用标志的信息,请参阅 influx auth create 文档

使用 InfluxDB API 创建令牌

使用 /api/v2/authorizations InfluxDB API 端点创建令牌。

POST http://localhost:8086/api/v2/authorizations

在您的请求中包含以下内容

要求包含方式
具有 write: authorizations 权限的 API 令牌使用 Authorization 标头和 BearerToken 方案。
组织在请求正文中作为 orgID 传递。
权限列表在请求正文中作为 permissions 数组传递。
INFLUX_ORG_ID=YOUR_ORG_ID
INFLUX_TOKEN=YOUR_API_TOKEN

curl -v --request POST \
  http://localhost:8086/api/v2/authorizations \
  --header "Authorization: Token ${INFLUX_TOKEN}" \
  --header 'Content-type: application/json' \
  --data '{
  "status": "active",
  "description": "iot-center-device",
  "orgID": "'"${INFLUX_ORG_ID}"'",
  "permissions": [
    {
      "action": "read",
      "resource": {
        "orgID": "'"${INFLUX_ORG_ID}"'",
        "type": "authorizations"
      }
    },
    {
      "action": "read",
      "resource": {
        "orgID": "'"${INFLUX_ORG_ID}"'",
        "type": "buckets"
      }
    },
    {
      "action": "write",
      "resource": {
        "orgID": "'"${INFLUX_ORG_ID}"'",
        "type": "buckets",
        "name": "iot-center" 
      }
    }
  ]
}'

创建作用域限定为用户的令牌

要将令牌的作用域限定为令牌创建者以外的用户,请在请求正文中传递 userID 属性。

######################################################
# The example below uses common command-line tools 
# `curl`, `jq` with the InfluxDB API to do the following:
# 1. Create a user.
# 2. Find the new or existing user by name.
# 3. If the user exists:
#   a. Build an authorization object with the user ID.
#   b. Create the new authorization.
#   c. Return the new token.
######################################################

INFLUX_ORG_ID=YOUR_ORG_ID
INFLUX_TOKEN=YOUR_API_TOKEN

function create_token_with_user() {
  curl --request POST \
    "http://localhost:8086/api/v2/users/" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Content-type: application/json' \
    --data "{\"name\": \"$1\"}"
  
  curl --request GET \
    "http://localhost:8086/api/v2/users?name=$1" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Content-type: application/json' | \
  
  jq --arg USER $1 '.users[0] // error("User missing")
    | {
        "orgID": "'"${INFLUX_ORG_ID}"'",
        "userID": .id,
        "description": $USER,
        "permissions": [
           {"action": "read", "resource": {"type": "buckets"}}
         ]
      }' | \
  
  curl --request POST \
    "http://localhost:8086/api/v2/authorizations" \
    --header "Authorization: Token ${INFLUX_TOKEN}" \
    --header 'Content-type: application/json' \
    --data @- | \
  
  jq '.token'
}

create_token_with_user 'iot_user_1'

有关选项的更多信息,请参阅 POST /api/v2/authorizations 文档


此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像现在一样使用它,而无需更改任何代码。

阅读更多

现已正式发布

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速的最新数据引擎,可实时收集和处理数据,并将其持久化到本地磁盘或对象存储。InfluxDB 3 Enterprise 构建在 Core 的基础上,增加了高可用性、读取副本、增强的安全性以及数据压缩功能,以实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看