|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |