ElasticSearch入门,解决missing authentication credentials for REST request

ElasticSearch(springBoot)

文章目录

  • ElasticSearch(springBoot)
    • 安装
    • 配置
    • 实际使用中一直报错: missing authentication credentials for REST request

安装

参考:https://blog.csdn.net/yjclsx/article/details/81302041

配置

1、这里需要在pom文件中引入以下内容:

 <!--elastic-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>

2、在Elasticsearch7.0后的版本里ElasticsearchTemple已经启用,7.x采用ElasticsearchRestTemple(ElasticsearchOperations)代替。使用ElasticsearchRestTemple需要写一段配置代码

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
  @Override
  public RestHighLevelClient elasticsearchClient() {       
    return RestClients.create(ClientConfiguration.localhost()).rest();
  }

  // no special bean creation needed                       
}

以上来自spring-data-elasticsearch官方文档:https://docs.spring.io/spring-data/elasticsearch/docs/4.0.3.RELEASE/reference/html/#elasticsearch.operations

实际使用中一直报错: missing authentication credentials for REST request

经过多方查证,最后发现报错原因是:配置ES时没添加用户名密码验证
修改后的代码:

@Component
@Configuration
@ConfigurationProperties(prefix = "spring.elasticsearch.rest")
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
    private String uris = "192.168.1.xxx:8080";
    private String username = "elastic";
    private String password = "xxxxxxx";
    @Override
    @Bean(destroyMethod = "close")
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(uris)
                .withBasicAuth(username, password)
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}

ElasticsearchRestTemplet具体使用后续更新。

你可能感兴趣的:(elasticsearch,elasticsearch)