BasicAuthorizationInterceptor belongs to http.client.support

Issue: SPR-14412
This commit is contained in:
Juergen Hoeller 2016-06-29 10:41:18 +02:00
parent 6cd85ddde7
commit 772bc030ee
2 changed files with 19 additions and 5 deletions

View File

@ -14,12 +14,15 @@
* limitations under the License.
*/
package org.springframework.http.client;
package org.springframework.http.client.support;
import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
@ -37,17 +40,25 @@ public class BasicAuthorizationInterceptor implements ClientHttpRequestIntercept
private final String password;
/**
* Create a new interceptor which adds a BASIC authorization header
* for the given username and password.
* @param username the username to use
* @param password the password to use
*/
public BasicAuthorizationInterceptor(String username, String password) {
Assert.hasLength(username, "Username must not be empty");
this.username = username;
this.password = (password == null ? "" : password);
this.password = (password != null ? password : "");
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
String token = Base64Utils
.encodeToString((this.username + ":" + this.password).getBytes(UTF_8));
String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(UTF_8));
request.getHeaders().add("Authorization", "Basic " + token);
return execution.execute(request, body);
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.http.client;
package org.springframework.http.client.support;
import java.net.URI;
@ -24,6 +24,9 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;