gpt4 book ai didi

spring-boot - Spring Boot使用基本身份验证连接到Elasticsearch

转载 作者:行者123 更新时间:2023-12-02 22:57:08 26 4
gpt4 key购买 nike

概述:

我尝试了API与我的本地Elasticsearch之间的非安全连接,并且一切正常。为了确保连接的安全性,我执行了以下步骤以在 flex 组件上添加xpack插件并在api中使用它:

  • 从 flex 部分开始,我基于Install xpack 安装了xpack,在Api之外一切都很好。然后添加用户名 api 和密码 apipass 作为新的 flex super 用户。
  • 从API方面,我针对安全连接更新了以下代码:
    Java Client and Security

  • 我的代码更改了 flex 配置:
    @Bean
    public TransportClient transportClient() throws UnknownHostException {
    Settings settings = Settings.builder()
    .put("cluster.name", clusterName)
    .put("xpack.security.user", "api:apipass")
    .build();
    try (TransportClient client = new PreBuiltXPackTransportClient(settings)
    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), tcpPort))) {
    String token = basicAuthHeaderValue("api", new SecureString("apipass".toCharArray()));
    client.filterWithHeader(Collections.singletonMap("Authorization", token))
    .prepareSearch().get();

    return client;
    }
    }

    问题:

    尝试通过应用程序运行 flex 查询时,将引发以下异常:
    Caused by: java.lang.IllegalStateException: transport client is closed
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:243) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:357) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46) ~[elasticsearch-6.0.0.jar:6.0.0]
    at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53) ~[elasticsearch-6.0.0.jar:6.0.0]

    如果有人可以与我分享他们真正的解决方案,以使用基本身份验证将Spring Boot应用程序连接到Elasticsearch,我将不胜感激?

    最佳答案

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
    new UsernamePasswordCredentials("username", "password"));

    final RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200))
    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
    @Override
    public HttpAsyncClientBuilder customizeHttpClient(
    HttpAsyncClientBuilder httpClientBuilder) {
    httpClientBuilder.disableAuthCaching();
    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }
    });

    final RestClient restClient = builder.build();
    final Response result = restClient.performRequest("GET", "_index/blahblah",
    Collections.emptyMap());

    @saeedj:我关注了 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.x/_basic_authentication.html

    你可以试试看 :)

    关于spring-boot - Spring Boot使用基本身份验证连接到Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49484565/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com