Enable empty username and password in ElasticsearchHibernatePropertiesBuilder
This commit is contained in:
parent
0ccd71faf8
commit
c6aa21b915
|
@ -20,6 +20,7 @@ package ca.uhn.fhir.jpa.search.lastn;
|
||||||
* #L%
|
* #L%
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.auth.AuthScope;
|
import org.apache.http.auth.AuthScope;
|
||||||
|
@ -31,6 +32,8 @@ import org.elasticsearch.client.RestClient;
|
||||||
import org.elasticsearch.client.RestClientBuilder;
|
import org.elasticsearch.client.RestClientBuilder;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class ElasticsearchRestClientFactory {
|
public class ElasticsearchRestClientFactory {
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,20 +55,21 @@ public class ElasticsearchRestClientFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static public RestHighLevelClient createElasticsearchHighLevelRestClient(String theHostname, int thePort, String theUsername, String thePassword) {
|
static public RestHighLevelClient createElasticsearchHighLevelRestClient(
|
||||||
final CredentialsProvider credentialsProvider =
|
String theHostname, int thePort, @Nullable String theUsername, @Nullable String thePassword) {
|
||||||
new BasicCredentialsProvider();
|
|
||||||
credentialsProvider.setCredentials(AuthScope.ANY,
|
|
||||||
new UsernamePasswordCredentials(theUsername, thePassword));
|
|
||||||
RestClientBuilder clientBuilder = RestClient.builder(
|
|
||||||
new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)))
|
|
||||||
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
|
|
||||||
.setDefaultCredentialsProvider(credentialsProvider));
|
|
||||||
|
|
||||||
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
|
RestClientBuilder clientBuilder = RestClient.builder(
|
||||||
clientBuilder.setDefaultHeaders(defaultHeaders);
|
new HttpHost(stripHostOfScheme(theHostname), thePort, determineScheme(theHostname)));
|
||||||
|
if (StringUtils.isNotBlank(theUsername) && StringUtils.isNotBlank(thePassword)) {
|
||||||
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
|
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(theUsername, thePassword));
|
||||||
|
clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
|
||||||
|
.setDefaultCredentialsProvider(credentialsProvider));
|
||||||
|
}
|
||||||
|
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
|
||||||
|
clientBuilder.setDefaultHeaders(defaultHeaders);
|
||||||
|
|
||||||
return new RestHighLevelClient(clientBuilder);
|
return new RestHighLevelClient(clientBuilder);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue