http请求Elasticsearch7中Xpack保护数据,用户名密码,curl转http

http请求Elasticsearch7中Xpack保护数据,用户名密码,curl转http

问题简介

由于之前一直用7之前的版本,es一直在本地裸奔,不用考虑安全问题。自从把集群放到服务器上了就升级了一下7.7,并且配置了xpack,
访问的链接就要变化了。不能直接get请求http://ip:port/index/type/id 的url数据了
一开始以为传两个参数模拟一下post请求就可以了,但是研究一番,发现事情并没有那么简单。
解决思路

  1. 官网上提供了curl携带用户名密码的访问方式
  2. 把curl请求转为http请求不就完事了

例如官网给出的bulk 文件的URL中:
例如bulk文件时:curl -u username:password -H ‘Content-Type:application/x-ndjson’ -XPOST ‘123.456.789.111:9200/index_name/_bulk?pretty’ --data-binary @bulk.json

解决办法

直接上代码:

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;

GET请求

public static void get() throws Exception {
		// es7之后去掉了type,但是type依然存在,只是为默认值“_doc”,通过id查询时要加上
		//换上自己的url
        String stringUrl = "http://123.45.67.890:9200/index_name/_doc/123456";
        URL url = new URL(stringUrl);
        URLConnection uc = url.openConnection();
        HttpURLConnection conn = (HttpURLConnection) uc;
        // 替换为自己的 username 和 password 
        String userpass = "your_username" + ":" + "your_password";
        byte[] encode = Base64.getEncoder().encode(userpass.getBytes("utf-8"));
        String authString = new String(encode);
        // Basic后面的空格必不可少
        String basicAuth = "Basic " + authString;
        conn.setRequestProperty("Authorization", basicAuth);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.connect();
        System.out.println(conn.getResponseCode());
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int len;
        byte[] arr = new byte[1024];
        while((len=bis.read(arr))!= -1) {
            bos.write(arr, 0, len);
            bos.flush();
        }
        bos.close();
        // 打印一下结果,该咋处理自己操作把
        System.out.println(bos.toString("utf-8"));
    }

POST请求

    public static String post(String jsonText, String stringUrl) {
        try {
            URL url = new URL(stringUrl);
            URLConnection uc = url.openConnection();
            HttpURLConnection conn = (HttpURLConnection) uc;
            String userpass = "your_username" + ":" + "your_password";
            byte[] encode = Base64.getEncoder().encode(userpass.getBytes("utf-8"));
            String authString = new String(encode);
            String basicAuth = "Basic " + authString;
            conn.setRequestProperty("Authorization", basicAuth);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            OutputStream  out = conn.getOutputStream();
            // 写入请求的字符串
            out.write(jsonText.getBytes());
            out.flush();
            out.close();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] arr = new byte[1024];
            while((len=bis.read(arr))!= -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            bos.close();
            String result = bos.toString("utf-8");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

整明白了就点个赞呗!

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值