elasticsearch Sheld認証の2つの方式(http/java api)

4916 ワード

シーベルトの取り付け
第一歩install shield
 1.  bin/plugin install license
 2.  bin/plugin install shield
第二のステップ
 1. ps -ef | grep elastic
 2. kill -9 xxxx
 3. bin/elasticsearch
第三ステップadd admin
1. bin/shield/esusers useradd adminName -r admin
次にパスワードを2回入力します。
以下http認証を言います。
説明:http Clientからの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アプリ認証:
一つは、mavenプロジェクトであれば、直接Sheld 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>
第二修正セットニング
import org.elasticsearch.shield.ShieldPlugin;

Settings settings = Settings.settingsBuilder()
      .put("cluster.name", "yourclustername")
                   .put("shield.user","clusterusername:passwd")
                   .build();
ステップ3でclientを修正します。
Client client = TransportClient.builder()
        .addPlugin(ShieldPlugin.class)
                 .settings(settings).build()
                 .addTransportAddress(new      
InetSocketTransportAddress(InetAddress.getByName("yourhost"), 9300));
間違ったところがあれば、ご指摘ください。