Package org.springframework.http.client.support

# Class BasicAuthenticationInterceptor

java.lang.Object
org.springframework.http.client.support.BasicAuthenticationInterceptor

All Implemented Interfaces:
  `ClientHttpRequestInterceptor`

public class BasicAuthenticationInterceptor extends Object implements ClientHttpRequestInterceptor
`ClientHttpRequestInterceptor` to apply a given HTTP Basic Authentication username/password pair, unless a custom `Authorization` header has already been set.

Since:
  5.1.1
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `HttpHeaders.setBasicAuth(java.lang.String, java.lang.String)`
  - `HttpHeaders.AUTHORIZATION`

## Constructor Summary

Constructors
Constructor
Description
`BasicAuthenticationInterceptor(String username, String password)`
Create a new interceptor which adds Basic Authentication for the given username and password.
`BasicAuthenticationInterceptor(String username, String password, Charset charset)`
Create a new interceptor which adds Basic Authentication for the given username and password, encoded using the specified charset.

## Method Summary

Modifier and Type
Method
Description
`ClientHttpResponse`
`intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)`
Intercept the given request, and return a response.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### BasicAuthenticationInterceptor

public BasicAuthenticationInterceptor(String username, String password)
Create a new interceptor which adds Basic Authentication for the given username and password.

Parameters:
  `username` - the username to use
  `password` - the password to use
See Also:
  - `HttpHeaders.setBasicAuth(String, String)`
  - `HttpHeaders.encodeBasicAuth(String, String, Charset)`

### BasicAuthenticationInterceptor

public BasicAuthenticationInterceptor(String username, String password, @Nullable Charset charset)
Create a new interceptor which adds Basic Authentication for the given username and password, encoded using the specified charset.

Parameters:
  `username` - the username to use
  `password` - the password to use
  `charset` - the charset to use
See Also:
  - `HttpHeaders.setBasicAuth(String, String, Charset)`
  - `HttpHeaders.encodeBasicAuth(String, String, Charset)`

## Method Details

### intercept

public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException
Description copied from interface: `ClientHttpRequestInterceptor`
Intercept the given request, and return a response. The given `ClientHttpRequestExecution` allows the interceptor to pass on the request and response to the next entity in the chain.

A typical implementation of this method would follow the following pattern:

1. Examine the request and body.
2. Optionally wrap the request to filter HTTP attributes.
3. Optionally modify the body of the request.

- either execute the request using `ClientHttpRequestExecution.execute(HttpRequest, byte[])`
- or do not execute the request to block the execution altogether

4. Optionally wrap the response to filter HTTP attributes.

Note: if the interceptor throws an exception after receiving a response, it must close the response via `ClientHttpResponse.close()`.

Specified by:
  `intercept` in interface `ClientHttpRequestInterceptor`
Parameters:
  `request` - the request, containing method, URI, and headers
  `body` - the body of the request
  `execution` - the request execution
Returns:
  the response
Throws:
  `IOException` - in case of I/O errors