16
16
17
17
package org .springframework .boot .autoconfigure .elasticsearch ;
18
18
19
+ import java .net .URI ;
19
20
import java .time .Duration ;
20
21
21
22
import org .apache .http .HttpHost ;
43
44
* @author Brian Clozel
44
45
* @author Stephane Nicoll
45
46
* @author Vedran Pavic
47
+ * @author Evgeniy Cheban
46
48
*/
47
49
class ElasticsearchRestClientConfigurations {
48
50
@@ -58,7 +60,7 @@ RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRest
58
60
@ Bean
59
61
RestClientBuilder elasticsearchRestClientBuilder (ElasticsearchRestClientProperties properties ,
60
62
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 );
62
64
RestClientBuilder builder = RestClient .builder (hosts );
63
65
builder .setHttpClientConfigCallback ((httpClientBuilder ) -> {
64
66
builderCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (httpClientBuilder ));
@@ -72,6 +74,12 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
72
74
return builder ;
73
75
}
74
76
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
+
75
83
}
76
84
77
85
@ Configuration (proxyBeanMethods = false )
@@ -124,15 +132,32 @@ public void customize(RestClientBuilder builder) {
124
132
125
133
@ Override
126
134
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
+ });
127
143
map .from (this .properties ::getUsername ).whenHasText ().to ((username ) -> {
128
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
129
144
Credentials credentials = new UsernamePasswordCredentials (this .properties .getUsername (),
130
145
this .properties .getPassword ());
131
146
credentialsProvider .setCredentials (AuthScope .ANY , credentials );
132
- builder .setDefaultCredentialsProvider (credentialsProvider );
133
147
});
134
148
}
135
149
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
+
136
161
@ Override
137
162
public void customize (RequestConfig .Builder builder ) {
138
163
map .from (this .properties ::getConnectionTimeout ).whenNonNull ().asInt (Duration ::toMillis )
0 commit comments