BasicAuthorizationInterceptor belongs to http.client.support
Issue: SPR-14412
This commit is contained in:
parent
6cd85ddde7
commit
772bc030ee
|
|
@ -14,12 +14,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.http.client;
|
package org.springframework.http.client.support;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.springframework.http.HttpRequest;
|
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.Assert;
|
||||||
import org.springframework.util.Base64Utils;
|
import org.springframework.util.Base64Utils;
|
||||||
|
|
||||||
|
|
@ -37,17 +40,25 @@ public class BasicAuthorizationInterceptor implements ClientHttpRequestIntercept
|
||||||
|
|
||||||
private final String password;
|
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) {
|
public BasicAuthorizationInterceptor(String username, String password) {
|
||||||
Assert.hasLength(username, "Username must not be empty");
|
Assert.hasLength(username, "Username must not be empty");
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = (password == null ? "" : password);
|
this.password = (password != null ? password : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||||
ClientHttpRequestExecution execution) throws IOException {
|
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);
|
request.getHeaders().add("Authorization", "Basic " + token);
|
||||||
return execution.execute(request, body);
|
return execution.execute(request, body);
|
||||||
}
|
}
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.http.client;
|
package org.springframework.http.client.support;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
|
|
@ -24,6 +24,9 @@ import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import org.springframework.beans.DirectFieldAccessor;
|
import org.springframework.beans.DirectFieldAccessor;
|
||||||
import org.springframework.http.HttpMethod;
|
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.junit.Assert.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
Loading…
Reference in New Issue