gpt4 book ai didi

javascript - REST请求Elasticsearch缺少身份验证 token

转载 作者:行者123 更新时间:2023-12-03 00:04:57 25 4
gpt4 key购买 nike

这是我第一次在这里发布问题:我正在AngularJS中创建一个前端网站,在NodeJS中创建一个后端网站。该网站应该向我提供有关我的 flex 搜索集群的信息,并从Elasticsearch索引中获取一些信息。我尝试使用Elasticsearch Javascript API来执行请求,但是它不起作用。

我正在使用ElasticSearch 5.4

这是request的示例:

var client = new elasticsearch.Client ({

host: 'https://noev02pe.fr:9200',
auth: 'user:password',
log: 'trace',
headers: {
'Authorization': 'Basic user:password',
}
});

export function connect() {
client.search({
index: 'metric-prod*',
q: 'kafka'
}
, function (error, response) {
console.log(response);
});
}

控制台上的响应是:
{ error:
{ root_cause: [ [Object] ],
type: 'security_exception',
reason: 'missing authentication token for REST request [/metric-
prod*/_search?q=kafka]',
header: { 'WWW-Authenticate': 'Basic realm="security" charset="UTF-8"'
} },
status: 401 }

我也尝试做经典的帖子请求:
   export function createUser(request,response,next){
var username = request.params.username;
var userData = querystring.stringify(request.body);
console.log(userData);
var options ={
hostname: 'noev02vr.fr',
port: 9200,
rejectUnauthorized: false,
path: "_xpack/security/user/"+username,
method:'POST',
headers: {
'Authorization': 'Basic ' + prodPass,
'Content-Type': 'application/json',
'Content-Length': userData.length
}
};



var post_req=http.request(options, function(res){
console.log('post user reussi');
res.on('data', function(data){

response.writeHead(res.statusCode);
response.write(data);
console.log(res.statusCode);
});

});

post_req.write(userData);
post_req.end();

}

我得到一个500错误。

最佳答案

基本认证

const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200', //Replace with your URL.
auth: {
username: 'elastic',
password: '*****' //Replace with your password
}
})
否则,您可以在节点URL中提供您的凭据
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://username:password@localhost:9200'
})
如果您启用了ssl,则这是配置
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200',
auth: {
username: 'elastic',
password: '*****'
},
ssl: {
ca: fs.readFileSync('./cacert.pem'),
rejectUnauthorized: false
}
})
您可以获取您的用户名和密码,请引用此链接
https://www.elastic.co/guide/en/cloud-enterprise/current/ece-password-reset-elastic.html

关于javascript - REST请求Elasticsearch缺少身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45731365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com