ElasticSearch添加x-pack認(rèn)證

1.配置ElasticSearch

  • 編輯elasticsearch.yml
# 集群名
cluster.name: hp-application
# 節(jié)點(diǎn)名
node.name: Mcgrady
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true

xpack.security.transport.ssl.enabled: true
# 允許外網(wǎng)訪問
network.host: 0.0.0.0
# 設(shè)置network.host后必須添加如下配置,否則報(bào)錯(cuò)
cluster.initial_master_nodes: ["Mcgrady"]
  • 設(shè)置訪問密碼
    在bin目錄下使用如下命令elasticsearch-setup-passwords interactive修改預(yù)置賬號(hào)的密碼
    根據(jù)提示設(shè)置多組密碼后完成配置
elastic:超級(jí)管理員賬號(hào)
kibana:Kibana訪問專用賬號(hào)logstash_system:Logstash訪問專用賬號(hào)
beats_system:FileBeat訪問專用賬號(hào)apm_system:APM系統(tǒng)專用賬號(hào)
remote_monitoring_user:遠(yuǎn)程監(jiān)控賬號(hào)

2.配置Kibana

編輯kibana.yml

elasticsearch.hosts: ["http://localhost:9200"]
elasticsearch.username: "elastic"
elasticsearch.password: "Zssy2020."

3.客戶端使用

  • Kibana
    重啟ElasticSearch、Kianaba后訪問Kiabana要求輸入密碼
  • SpringBoot ElasticSearch high level search API
elasticsearch:
  cluster-name: elasticsearch
  cluster-nodes:
    - 192.168.0.113:9200
  account:
    username: elastic
    password: Zssy2020.
  index:
    number-of-replicas: 0
    number-of-shards: 3

@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfig {

    @Autowired
    private ElasticsearchProperties elasticsearchProperties;

    private final List<HttpHost> httpHosts = new ArrayList<>();

    @Bean(destroyMethod = "close")
    @ConditionalOnMissingBean
    public RestHighLevelClient restHighLevelClient() {
        List<String> clusterNodes = elasticsearchProperties.getClusterNodes();
        if (clusterNodes.isEmpty()) {
            throw new RuntimeException("集群節(jié)點(diǎn)不允許為空");
        }
        clusterNodes.forEach(node -> {
            try {
                String[] parts = StringUtils.split(node, ":");
                Assert.notNull(parts, "Must defined");
                Assert.state(parts.length == 2, "Must be defined as 'host:port'");
                httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema()));
            } catch (Exception e) {
                throw new IllegalStateException("Invalid ES nodes " + "property '" + node + "'", e);
            }
        });
        RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0]));
        return getRestHighLevelClient(builder, elasticsearchProperties);
    }

    /**
     * get restHistLevelClient
     */
    private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) {
        builder.setRequestConfigCallback(requestConfigBuilder -> {
            requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout());
            requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout());
            requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout());
            return requestConfigBuilder;
        });
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal());
            httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute());
            return httpClientBuilder;
        });
        // 認(rèn)證回調(diào)
        ElasticsearchProperties.Account account = elasticsearchProperties.getAccount();
        if (!StringUtils.isEmpty(account.getUsername()) && !StringUtils.isEmpty(account.getUsername())) {
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(account.getUsername(), account.getPassword()));
            builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
        return new RestHighLevelClient(builder);
    }
}

4.可能出現(xiàn)的錯(cuò)誤

  1. ElasticSearch配置錯(cuò)誤
the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

解決辦法:
放開配置 cluster.initial_master_nodes: ["node-1", "node-2"]

參考:
http://www.itdecent.cn/p/428fbf37993e

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容