Skip to content

Commit f399b12

Browse files
committed
Support userInfo in elasticsearch uri
Fixes gh-21291
1 parent 7afd25f commit f399b12

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.elasticsearch;
1818

19+
import java.net.URI;
1920
import java.time.Duration;
2021

2122
import org.apache.http.HttpHost;
@@ -43,6 +44,7 @@
4344
* @author Brian Clozel
4445
* @author Stephane Nicoll
4546
* @author Vedran Pavic
47+
* @author Evgeniy Cheban
4648
*/
4749
class ElasticsearchRestClientConfigurations {
4850

@@ -58,7 +60,7 @@ RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRest
5860
@Bean
5961
RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperties properties,
6062
ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) {
61-
HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
63+
HttpHost[] hosts = properties.getUris().stream().map(this::createHttpHost).toArray(HttpHost[]::new);
6264
RestClientBuilder builder = RestClient.builder(hosts);
6365
builder.setHttpClientConfigCallback((httpClientBuilder) -> {
6466
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder));
@@ -72,6 +74,12 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
7274
return builder;
7375
}
7476

77+
private HttpHost createHttpHost(String uri) {
78+
URI parsedUri = URI.create(uri);
79+
String userInfo = parsedUri.getUserInfo();
80+
return HttpHost.create((userInfo != null) ? uri.replace(userInfo + "@", "") : uri);
81+
}
82+
7583
}
7684

7785
@Configuration(proxyBeanMethods = false)
@@ -124,15 +132,32 @@ public void customize(RestClientBuilder builder) {
124132

125133
@Override
126134
public void customize(HttpAsyncClientBuilder builder) {
135+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
136+
builder.setDefaultCredentialsProvider(credentialsProvider);
137+
this.properties.getUris().stream().map(URI::create).filter((uri) -> uri.getUserInfo() != null)
138+
.forEach((uri) -> {
139+
AuthScope authScope = new AuthScope(uri.getHost(), uri.getPort());
140+
Credentials credentials = createCredentials(uri.getUserInfo());
141+
credentialsProvider.setCredentials(authScope, credentials);
142+
});
127143
map.from(this.properties::getUsername).whenHasText().to((username) -> {
128-
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
129144
Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(),
130145
this.properties.getPassword());
131146
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
132-
builder.setDefaultCredentialsProvider(credentialsProvider);
133147
});
134148
}
135149

150+
private Credentials createCredentials(String usernameAndPassword) {
151+
int delimiter = usernameAndPassword.indexOf(":");
152+
if (delimiter == -1) {
153+
return new UsernamePasswordCredentials(usernameAndPassword, null);
154+
}
155+
156+
String username = usernameAndPassword.substring(0, delimiter);
157+
String password = usernameAndPassword.substring(delimiter + 1);
158+
return new UsernamePasswordCredentials(username, password);
159+
}
160+
136161
@Override
137162
public void customize(RequestConfig.Builder builder) {
138163
map.from(this.properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis)

0 commit comments

Comments
 (0)