1、在ES服务器创建:
curl --cacert elasticsearch-8.10.2/config/certs/http_ca.crt -u <username>:<password> -k -X POST "https://localhost:9200/_security/api_key" -H 'Content-Type: application/json' -d '{
"name": "my-api-key",
"expiration": "1d", // 设置 API Key 的过期时间,例如 "1d" 表示一天
"realm_name": "native1" // 指定用于创建 API Key 的认证域
}'
在上述命令中,my-api-key 是为 API Key 指定的名称。expiration 参数用于设置 API Key 的过期时间,1d 表示一天,可以根据需要进行调整。realm_name 参数指定用于创建 API Key 的认证域。
2、在Kibana控制台创建:
curl -XPOST 'https://localhost:9200/_security/api_key' -H 'Content-Type: application/json' -d '
{
"name": "ES-api-key","
expiration": "1d"
}'
创建 API Key 成功后,会得到一个包含 API Key 值的 JSON 对象,类似于:
{
"id": "<api-key-id>",
"name": "my-api-key",
"expiration": "2023-11-22T12:00:00Z",
"api_key": "<api-key-value>"
}
使用API Key:
curl -k -H "Authorization: ApiKey NFRjanBZd0JEN1hua0x6d1MtTzc6cFQtbHk0Z25TYXF3eHRjSzhKR0pRdw==" https://localhost:9200/_cluster/health
官方教程:
Create API key API | Elasticsearch Guide [8.11] | Elastic
Elasticsearch:创建 API key 接口访问 Elasticsearch_elasticsearch apikey-CSDN博客