• 在RestHighLevelClient中增加用户名密码验证


    @Configuration
    public class ElasticSearchClient {
        @Value("${ElasticSearch.Hosts}")
        private String hosts;
        @Value("${ElasticSearch.UserName}")
        private String userName;
        @Value("${ElasticSearch.Password}")
        private String password;
        
        @SuppressWarnings("deprecation")
        @Bean(name="esClient")
        public RestHighLevelClient getClient() {
            String[] hosts = this.hosts.split(",");
            HttpHost[] httpHosts = new HttpHost[hosts.length];
            for(int i=0;i<hosts.length;i++) {
                httpHosts[i] = new HttpHost(hosts[i], 9200, "http");
            }
            
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
            
            RestClientBuilder builder = RestClient.builder(httpHosts).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                @Override
                public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                    requestConfigBuilder.setConnectTimeout(-1);
                    requestConfigBuilder.setSocketTimeout(-1);
                    requestConfigBuilder.setConnectionRequestTimeout(-1);
                    return requestConfigBuilder;
                }
            }).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    httpClientBuilder.disableAuthCaching();
                    return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                }
            }).setMaxRetryTimeoutMillis(5*60*1000);
            
            RestHighLevelClient client = new RestHighLevelClient(builder);
            return client;
        }
    }
    
    自律人的才是可怕的人
  • 相关阅读:
    js——获取指定日期的前7天连续日期
    一个div自动充满当前屏幕的解决方法
    Cookie和Session
    Web服务器和浏览器间的工作原理
    软件测试基础概念
    K短路的几种求法
    ID字体
    搭建GitLab的Hexo博客记录
    项目selfcmp开发学习
    牛顿迭代法学习笔记
  • 原文地址:https://www.cnblogs.com/lovelifest/p/14362212.html
Copyright © 2020-2023  润新知