elasticsearch shield 认证的两种方式(http/java api)

安装shield

第一步 install shield

 1.  bin/plugin install license
 2.  bin/plugin install shield

第二步 restart elasticsearch

 1. ps -ef | grep elastic
 2. kill -9 xxxx
 3. bin/elasticsearch

第三步 add admin

1. bin/shield/esusers useradd adminName -r admin

然后输入 两次密码

下面说下 http 认证:

说明:我用的是 httpClient 发的post 请求 主要代码如下:

httpClient.getState().setCredentials(new AuthScope(host,ip),
                new UsernamePasswordCredentials(clusterusername,clusterpasswd));
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestEntity(new StringRequestEntity(param,contentType,charset));
            resNum = httpClient.executeMethod(postMethod);

java api 认证 :

一 如果是maven 项目 直接引入shield jar

<repositories>
      
      <repository>
         <id>elasticsearch-releasesid>
         <url>https://maven.elasticsearch.org/releasesurl>
         <releases>
            <enabled>trueenabled>
         releases>
         <snapshots>
            <enabled>falseenabled>
         snapshots>
      repository>
   repositories>
<dependency>
      <groupId>org.elasticsearch.plugingroupId>
      <artifactId>shieldartifactId>
      <version>2.3.5version>
    dependency>

第二 修改settings

import org.elasticsearch.shield.ShieldPlugin;

Settings settings = Settings.settingsBuilder()
      .put("cluster.name", "yourclustername")
                   .put("shield.user","clusterusername:passwd")
                   .build();

第三步 修改 client

Client client = TransportClient.builder()
        .addPlugin(ShieldPlugin.class)
                 .settings(settings).build()
                 .addTransportAddress(new      
InetSocketTransportAddress(InetAddress.getByName("yourhost"), 9300));

如有不对的地方,欢迎指正!

你可能感兴趣的:(es)