安装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>
<!-- add the elasticsearch repo -->
<repository>
<id>elasticsearch-releases</id>
<url>https://maven.elasticsearch.org/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>shield</artifactId>
<version>2.3.5</version>
</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));
如有不对的地方,欢迎指正!