Spring 中的 Elasticsearch HTTP 身份验证

我想访问一个受用户名和密码保护的远程弹性搜索.https://[用户名]:[密码]]@aws-eu-west-1-portal1.dblayer.com:11109/

I want to access a remote elasticsearch which is protected by a username and password. https://[username]:[password]@aws-eu-west-1-portal1.dblayer.com:11109/

在 Spring 中使用 XML 配置我能够访问我的本地主机弹性,如下所示

In Spring using the XML config I was able to access my localhost elastic as shown below

<!-- ElasticSearch -->
<elasticsearch:repositories base-package="be.smartsearch.service.repository.elasticsearch" />

<elasticsearch:transport-client id="esClient" cluster-nodes="localhost:9300" />

<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
  <constructor-arg name="client" ref="esClient" />
</bean>

到目前为止,我发现的唯一有用的文档是 PHP:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_security.html

The only usefull documentation I found so far is for PHP: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_security.html

如何使用 XML 配置的 Spring 数据中的凭据连接到远程 elasticsearh?

How can I connect to a remote elasticsearh with credentials in Spring data with the XML config?

在 Mongo 中,我可以通过以下方法做到这一点

In Mongo I was able to do it by the following method

<!-- Mongo -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}"/>

<mongo:db-factory dbname="SmartSearchAfterDemo" mongo-ref="mongo" username="${mongo.user}" password="${mongo.password}"/>
<!--<mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo"/> -->

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

<mongo:repositories base-package="be.smartsearch.service.repository.mongo"/>

推荐答案

Spring Data Elasticsearch 基于官方的 Elasticsearch Java Client,它使用二进制传输协议(而不是像 PHP 那样的 REST HTTP 协议).

Spring Data Elasticsearch is base on the official Elasticsearch Java Client which uses the binary Transport procol (not the REST HTTP procol like PHP).

如果您使用 Shield 来保护您的 Elasticsearch,那么您可以在传输客户端/传输协议上设置用户/密码

If you're using Shield to secure your Elasticsearch, then you can set the user/password on the Transport client/Transport procol

TransportClient client = TransportClient.builder()
    .addPlugin(ShieldPlugin.class)
    .settings(Settings.builder()
        .put("cluster.name", "yourcluster")
        .put("shield.user", "youruser:yourpassword")
        ...
        .build())

如果你不想在 Java 代码中使用 HTTP 协议,那么社区客户端:

If you wan't to use the HTTP protocol from Java code then there are to community clients:

  • Jest 支持 HTTP 身份验证
  • Elasticsearch HTTP 这是相当新的
  • Jest which supports HTTP authentication
  • Elasticsearch HTTP which is pretty new

但是这些解决方案与 Spring Data 不兼容

But these solutions are not compatible with Spring Data

相关文章