Spring中的Elasticsearch HTTP身份验证 [英] Elasticsearch HTTP authentication in Spring
问题描述
我想访问受用户名和密码保护的远程Elasticsearch.
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配置,我可以访问我的localhost弹性,如下所示
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/zh-CN/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客户端,该客户端使用二进制Transport procol(而不是像PHP那样的REST HTTP procol).
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,则可以在Transport客户端/Transport协议上设置用户名/密码
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:
- 笑话支持 Elasticsearch HTTP ,这是很新的
- Jest which supports HTTP authentication
- Elasticsearch HTTP which is pretty new
但是这些解决方案与Spring Data不兼容
But these solutions are not compatible with Spring Data
这篇关于Spring中的Elasticsearch HTTP身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!