Spring 中的 Elasticsearch HTTP 身份验证

2024-08-23Java开发问题
9

本文介绍了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

这篇关于Spring 中的 Elasticsearch HTTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13