gpt4 book ai didi

jquery - Elasticsearch : Shield Authentication not working in AJAX call

转载 作者:行者123 更新时间:2023-11-29 02:50:13 39 4
gpt4 key购买 nike

我正在尝试使用屏蔽身份验证向 elasticsearch 发送 ajax 调用

$.ajax({
url: 'http://localhost/test2/test/_search',
type: 'POST',
//contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'json',
username: "admin",
password: "admin123",
data: JSON.stringify(queryBody),
success: function(response) {
alert(response)
var data = response.hits.hits;
var titleArray = [];
//alert(data.length);
if (data.length > 0) {
/*
if (data.length > 5)
data.length=5;
*/
for (var i = 0; i < data.length; i++) {
if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1)
{
titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]);
}
}
responseS(titleArray);
titleArray=[];
} else { }
},
error: function(jqXHR, textStatus, errorThrown) {
var jso = jQuery.parseJSON(jqXHR.responseText);
alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error);
}
});

但我得到:

POST http://localhost:9200/test2/test/_search 401(Unauthorized)

我也试过:

$.ajax({
url: 'http://admin:admin123@localhost/test2/test/_search',
type: 'POST',
//contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'json',
data: JSON.stringify(queryBody),
success: function(response) {
alert(response)
var data = response.hits.hits;

var titleArray = [];

//alert(data.length);
if (data.length > 0) {
/*
if (data.length > 5)
data.length=5;
*/
for (var i = 0; i < data.length; i++) {

if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1)
{

titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]);
}
}

responseS(titleArray);
titleArray=[];

} else {

}


},

error: function(jqXHR, textStatus, errorThrown) {
var jso = jQuery.parseJSON(jqXHR.responseText);
alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error);
}
});

但我遇到了同样的 401 错误。

接下来我尝试了:

    $.ajax({
url: 'http://localhost/test2/test/_search',
type: 'POST',
//contentType: 'application/json; charset=UTF-8',
crossDomain: true,
dataType: 'json',
data: JSON.stringify(queryBody),
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa("admin:admin123"));
}, success: function(response) {
alert(response)
var data = response.hits.hits;

var titleArray = [];

//alert(data.length);
if (data.length > 0) {
/*
if (data.length > 5)
data.length=5;
*/
for (var i = 0; i < data.length; i++) {

if(data[i].fields.Title[0].indexOf(settings.fieldValue) > -1)
{

titleArray.push(data[i].fields.DocumentID[0]+":"+data[i].fields.Title[0]);
}
}

responseS(titleArray);
titleArray=[];

} else {

}


},

error: function(jqXHR, textStatus, errorThrown) {
var jso = jQuery.parseJSON(jqXHR.responseText);
alert('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --<br />' + jso.error);
}
});

但现在我明白了

XMLHttpRequest cannot load http://localhost:9200/test2/test/_search. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response. localhost/:1 Uncaught SyntaxError: Unexpected token u

通过 ajax 调用将用户名和密码发送到 elastic 的正确方法是什么?

这是我的elasticsearch.yml

action.disable_delete_all_indices: true

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization, X-Requested-With, Content-Type, Content-Length"
http.cors.allow-credentials: true

bootstrap.mlockall: true

# For reference: https://www.elastic.co/guide/en/elasticsearch/guide/current/_limiting_memory_usage.html

# controls how much heap space is allocated to fielddata. When you run a query that requires access to new field values,
# it will load the values into memory and then try to add them to fielddata. If the resulting fielddata size would
# exceed the specified size, other values would be evicted in order to make space.
indices.fielddata.cache.size: 40%

# The fielddata circuit breaker limits the size of fielddata to 60% of the heap, by default.
indices.breaker.fielddata.limit: 60%

# The request circuit breaker estimates the size of structures required to complete other parts of a request,
# such as creating aggregation buckets, and limits them to 40% of the heap, by default.
indices.breaker.request.limit: 40%

# The total circuit breaker wraps the request and fielddata circuit breakers to ensure that the combination
# of the two doesn’t use more than 70% of the heap by default.
indices.breaker.total.limit: 70%
#shield.enabled: false
shield:
authc:
realms:
native1:
type: native
order: 0
realms:
esusers:
type: esusers
order: 1
files:
users: ElasticSearch\elasticsearch-2.3.1\elasticsearch-2.3.1\config\shield\users
users_roles: ElasticSearch\elasticsearch-2.3.1\elasticsearch-2.3.1\config\shield\users_role

最佳答案

解决此问题的方法是将 CORS 配置为在 elasticsearch.yml 文件中接受 Authorization header :

http.cors.allow-headers: "Authorization, X-Requested-With, Content-Type, Content-Length"

还要确保您的 elasticsearch.yml 文件中有以下三个设置:

http.cors.enabled: true
http.cors.allow-origin: /https?:\/\/localhost(:[0-9]+)?/
http.cors.allow-credentials: true

关于jquery - Elasticsearch : Shield Authentication not working in AJAX call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883936/

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