Browse Source

BasicAuthorizationInterceptor belongs to http.client.support

Issue: SPR-14412
pull/1095/head
Juergen Hoeller 9 years ago
parent
commit
772bc030ee
  1. 19
      spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java
  2. 5
      spring-web/src/test/java/org/springframework/http/client/support/BasicAuthorizationInterceptorTests.java

19
spring-web/src/main/java/org/springframework/http/client/BasicAuthorizationInterceptor.java → spring-web/src/main/java/org/springframework/http/client/support/BasicAuthorizationInterceptor.java

@ -14,12 +14,15 @@ @@ -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 @@ -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);
}

5
spring-web/src/test/java/org/springframework/http/client/BasicAuthorizationInterceptorTests.java → spring-web/src/test/java/org/springframework/http/client/support/BasicAuthorizationInterceptorTests.java

@ -14,7 +14,7 @@ @@ -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; @@ -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.*;
Loading…
Cancel
Save