在RestHighLevelClient中增加用户名密码验证
转载自https://www.cnblogs.com/ilovejesus/p/12579841.html@Configurationpublic class ElasticSearchClient {@Value("${ElasticSearch.Hosts}")private String hosts;@Value("${ElasticSearch.UserName}")private Str
·
转载自https://www.cnblogs.com/ilovejesus/p/12579841.html
@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;
}
}
更多推荐
已为社区贡献13条内容
所有评论(0)