curl -X GET "http://192.168.110.7:9200" {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\", charset=\"UTF-8\"","ApiKey"]}}],"type"
时间: 2025-05-05 17:07:10 浏览: 26
### 解决Elasticsearch `security_exception` 缺少身份验证凭据问题
当 Elasticsearch 配置启用了安全性功能(即 `xpack.security.enabled=true`),如果未提供有效的认证凭证,则会触发 `security_exception` 错误[^1]。错误消息表明 REST 请求缺少必要的身份验证凭据,通常表现为 HTTP 状态码 401 和详细的错误信息[^2]。
以下是解决问题的方法:
#### 方法一:重置默认用户密码
通过命令行工具可以为内置的超级管理员用户 `elastic` 设置新密码。执行以下命令指定目标节点和服务地址:
```bash
bin/elasticsearch-reset-password --url "https://<your-elasticsearch-host>:9200" --username elastic -i
```
上述命令中的 `<your-elasticsearch-host>` 应替换为目标主机的实际 IP 地址或域名[^3]。完成操作后记录生成的新密码用于后续的身份验证过程。
#### 方法二:配置 Logstash 输出插件支持基本认证
假设您正在使用 Logstash 将数据写入到受保护的 Elasticsearch 实例中,在定义输出部分时需加入用户名与密码字段来满足安全需求。例如修改如下所示的第一管道配置文件 (`first-pipeline.conf`) 中的相关片段即可实现这一目的:
```conf
output {
elasticsearch {
hosts => ["http://localhost:9200"]
user => "elastic"
password => "your-newly-set-password"
index => "aa-%{+YYYY.MM.dd}"
document_type => "wjb_log"
}
}
```
此处需要注意的是将 `"your-newly-set-password"` 替换为您之前创建或者更改后的实际密码值[^4]。
#### 方法三:集成 API 平台并应用基础认证机制
对于希望通过编程方式调用已开启权限控制之下的 Elasticsearch 接口的情况来说,可以在发起请求前附加相应的 Authorization Header 字段以传递 Base64 编码形式组合而成的字符串作为令牌值。具体做法可参照下面 Python 脚本实例演示如何利用 requests 库发送 PUT 请求的同时附加上述所需头部信息:
```python
import base64
import requests
def put_with_auth(url, username='elastic', password='your_password'):
auth_str = f'{username}:{password}'
b64_val = base64.b64encode(auth_str.encode()).decode()
headers = {'Authorization': f'Basic {b64_val}'}
response = requests.put(url, headers=headers)
return response.status_code, response.text
if __name__ == '__main__':
url_to_access = 'http://localhost:9200/_cluster/health'
status, content = put_with_auth(url=url_to_access)
print(f'Status Code: {status}\nContent:\n{content}')
```
同样记得把 `'your_password'` 更改为真实环境中适用的有效密钥[^5]。
---
阅读全文
相关推荐


















