17
17
package org .springframework .boot .autoconfigure .elasticsearch ;
18
18
19
19
import java .net .URI ;
20
+ import java .net .URISyntaxException ;
20
21
import java .time .Duration ;
21
22
22
23
import org .apache .http .HttpHost ;
23
24
import org .apache .http .auth .AuthScope ;
24
25
import org .apache .http .auth .Credentials ;
25
26
import org .apache .http .auth .UsernamePasswordCredentials ;
26
- import org .apache .http .client .CredentialsProvider ;
27
27
import org .apache .http .client .config .RequestConfig ;
28
28
import org .apache .http .impl .client .BasicCredentialsProvider ;
29
29
import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
37
37
import org .springframework .boot .context .properties .PropertyMapper ;
38
38
import org .springframework .context .annotation .Bean ;
39
39
import org .springframework .context .annotation .Configuration ;
40
+ import org .springframework .util .StringUtils ;
40
41
41
42
/**
42
43
* Elasticsearch rest client infrastructure configurations.
@@ -75,9 +76,25 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
75
76
}
76
77
77
78
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 );
79
+ try {
80
+ return createHttpHost (URI .create (uri ));
81
+ }
82
+ catch (IllegalArgumentException ex ) {
83
+ return HttpHost .create (uri );
84
+ }
85
+ }
86
+
87
+ private HttpHost createHttpHost (URI uri ) {
88
+ if (!StringUtils .hasLength (uri .getUserInfo ())) {
89
+ return HttpHost .create (uri .toString ());
90
+ }
91
+ try {
92
+ return HttpHost .create (new URI (uri .getScheme (), null , uri .getHost (), uri .getPort (), uri .getPath (),
93
+ uri .getQuery (), uri .getFragment ()).toString ());
94
+ }
95
+ catch (URISyntaxException ex ) {
96
+ throw new IllegalStateException (ex );
97
+ }
81
98
}
82
99
83
100
}
@@ -132,30 +149,7 @@ public void customize(RestClientBuilder builder) {
132
149
133
150
@ Override
134
151
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
- });
143
- map .from (this .properties ::getUsername ).whenHasText ().to ((username ) -> {
144
- Credentials credentials = new UsernamePasswordCredentials (this .properties .getUsername (),
145
- this .properties .getPassword ());
146
- credentialsProvider .setCredentials (AuthScope .ANY , credentials );
147
- });
148
- }
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 );
152
+ builder .setDefaultCredentialsProvider (new PropertiesCredentialsProvider (this .properties ));
159
153
}
160
154
161
155
@ Override
@@ -168,4 +162,47 @@ public void customize(RequestConfig.Builder builder) {
168
162
169
163
}
170
164
165
+ private static class PropertiesCredentialsProvider extends BasicCredentialsProvider {
166
+
167
+ PropertiesCredentialsProvider (ElasticsearchRestClientProperties properties ) {
168
+ if (StringUtils .hasText (properties .getUsername ())) {
169
+ Credentials credentials = new UsernamePasswordCredentials (properties .getUsername (),
170
+ properties .getPassword ());
171
+ setCredentials (AuthScope .ANY , credentials );
172
+ }
173
+ properties .getUris ().stream ().map (this ::toUri ).filter (this ::hasUserInfo )
174
+ .forEach (this ::addUserInfoCredentials );
175
+ }
176
+
177
+ private URI toUri (String uri ) {
178
+ try {
179
+ return URI .create (uri );
180
+ }
181
+ catch (IllegalArgumentException ex ) {
182
+ return null ;
183
+ }
184
+ }
185
+
186
+ private boolean hasUserInfo (URI uri ) {
187
+ return uri != null && StringUtils .hasLength (uri .getUserInfo ());
188
+ }
189
+
190
+ private void addUserInfoCredentials (URI uri ) {
191
+ AuthScope authScope = new AuthScope (uri .getHost (), uri .getPort ());
192
+ Credentials credentials = createUserInfoCredentials (uri .getUserInfo ());
193
+ setCredentials (authScope , credentials );
194
+ }
195
+
196
+ private Credentials createUserInfoCredentials (String userInfo ) {
197
+ int delimiter = userInfo .indexOf (":" );
198
+ if (delimiter == -1 ) {
199
+ return new UsernamePasswordCredentials (userInfo , null );
200
+ }
201
+ String username = userInfo .substring (0 , delimiter );
202
+ String password = userInfo .substring (delimiter + 1 );
203
+ return new UsernamePasswordCredentials (username , password );
204
+ }
205
+
206
+ }
207
+
171
208
}
0 commit comments