简体   繁体   English

通过JavaScript表单登录HTTP客户端到ElasticSearch(readonlyrest)

[英]Login HTTP client by JavaScript form to ElasticSearch (readonlyrest)

I want to login to my ElasticSearch via HTTP (with readonlyrest plugin installed). 我想通过HTTP(安装了readonlyrest插件)登录到我的ElasticSearch。

¿Is there any way to do this thing with an HTML form and JavaScript (Angular) code? ¿是否可以使用HTML表单和JavaScript(角度)代码来完成此操作?

<form class="form-horizontal">
      <div class="form-group">
        <label class="control-label col-sm-2" for="email">Username:</label>
        <div class="col-sm-10">
          <input class="form-control" ng-model="username" placeholder="Enter username">
        </div>
      </div>
      <div class="form-group">
        <label class="control-label col-sm-2" for="pwd">Password:</label>
        <div class="col-sm-10">
          <input type="password" class="form-control" placeholder="Enter password" ng-model="password">
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default" ng-click="login()">Submit</button>
        </div>
      </div>
</form>

Code of the login (I want to save the login in the browser for future actions): 登录代码(我要将登录信息保存在浏览器中以备将来使用):

$scope.login = function(){
console.log("Login: ", $scope.username, $scope.password)

var clientId = "sample";
var clientSecret = "sample";

var authorizationBasic = window.btoa(clientId + ':' + clientSecret);

var request = new XMLHttpRequest();
request.open('POST', "https://localhost:5601/omu/elasticsearch/_msearch", true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.setRequestHeader('Authorization', 'Basic ' + authorizationBasic);
request.setRequestHeader('Accept', 'application/json');
request.send("username=sample&password=sample&grant_type=password");

request.onreadystatechange = function () {
    if (request.readyState === 4) {
       alert(request.responseText);
    }
}}

Thx 谢谢

If you're using basic authentication, try to include the credentials in the path 如果您使用的是基本身份验证,请尝试在路径中包含凭据

GET http:// user:password @elastichost:9200/endpoint GET http:// 用户:密码 @elastichost:9200 / endpoint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM