scala連接elasticsearch並完成基本認證

def getRestClient(url: String): RestClient = {
      val restClientBuilder = RestClient.builder(HttpHost.create(url))
        val provider = new BasicCredentialsProvider()
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "password"))
        restClientBuilder.setHttpClientConfigCallback((httpClientBuilder: HttpAsyncClientBuilder) => {
          httpClientBuilder.disableAuthCaching()
          httpClientBuilder.setDefaultCredentialsProvider(provider)
        })
      val restClient = restClientBuilder.build()
      restClient
    }

參考文章:

https://www.bookstack.cn/read/elasticsearch-java-rest/java-low-level-rest-client-common-configuration-basic-authentication.md

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_other_authentication_methods.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章