1
1
/*
2
- * Copyright 2012-2016 the original author or authors.
2
+ * Copyright 2012-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
48
48
import org .springframework .http .client .ClientHttpRequestInterceptor ;
49
49
import org .springframework .http .client .ClientHttpResponse ;
50
50
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
51
- import org .springframework .http .client .InterceptingClientHttpRequestFactory ;
52
51
import org .springframework .http .client .support .BasicAuthorizationInterceptor ;
53
52
import org .springframework .util .Assert ;
54
53
import org .springframework .util .ClassUtils ;
@@ -144,11 +143,19 @@ private void addAuthentication(RestTemplate restTemplate, String username,
144
143
if (username == null ) {
145
144
return ;
146
145
}
147
- List <ClientHttpRequestInterceptor > interceptors = Collections
148
- .<ClientHttpRequestInterceptor >singletonList (
149
- new BasicAuthorizationInterceptor (username , password ));
150
- restTemplate .setRequestFactory (new InterceptingClientHttpRequestFactory (
151
- restTemplate .getRequestFactory (), interceptors ));
146
+ List <ClientHttpRequestInterceptor > interceptors = restTemplate .getInterceptors ();
147
+ if (interceptors == null ) {
148
+ interceptors = Collections .emptyList ();
149
+ }
150
+ interceptors = new ArrayList <ClientHttpRequestInterceptor >(interceptors );
151
+ Iterator <ClientHttpRequestInterceptor > iterator = interceptors .iterator ();
152
+ while (iterator .hasNext ()) {
153
+ if (iterator .next () instanceof BasicAuthorizationInterceptor ) {
154
+ iterator .remove ();
155
+ }
156
+ }
157
+ interceptors .add (new BasicAuthorizationInterceptor (username , password ));
158
+ restTemplate .setInterceptors (interceptors );
152
159
}
153
160
154
161
/**
@@ -985,8 +992,7 @@ public RestTemplate getRestTemplate() {
985
992
public TestRestTemplate withBasicAuth (String username , String password ) {
986
993
RestTemplate restTemplate = new RestTemplate ();
987
994
restTemplate .setMessageConverters (getRestTemplate ().getMessageConverters ());
988
- restTemplate .setInterceptors (
989
- removeBasicAuthInterceptorIfPresent (getRestTemplate ().getInterceptors ()));
995
+ restTemplate .setInterceptors (getRestTemplate ().getInterceptors ());
990
996
restTemplate .setRequestFactory (getRestTemplate ().getRequestFactory ());
991
997
restTemplate .setUriTemplateHandler (getRestTemplate ().getUriTemplateHandler ());
992
998
TestRestTemplate testRestTemplate = new TestRestTemplate (restTemplate , username ,
@@ -996,19 +1002,6 @@ public TestRestTemplate withBasicAuth(String username, String password) {
996
1002
return testRestTemplate ;
997
1003
}
998
1004
999
- private List <ClientHttpRequestInterceptor > removeBasicAuthInterceptorIfPresent (
1000
- List <ClientHttpRequestInterceptor > interceptors ) {
1001
- List <ClientHttpRequestInterceptor > updatedInterceptors = new ArrayList <ClientHttpRequestInterceptor >(
1002
- interceptors );
1003
- Iterator <ClientHttpRequestInterceptor > iterator = updatedInterceptors .iterator ();
1004
- while (iterator .hasNext ()) {
1005
- if (iterator .next () instanceof BasicAuthorizationInterceptor ) {
1006
- iterator .remove ();
1007
- }
1008
- }
1009
- return interceptors ;
1010
- }
1011
-
1012
1005
/**
1013
1006
* Options used to customize the Apache Http Client if it is used.
1014
1007
*/
0 commit comments