Top Description Fields Constructors Methods
org.springframework.http.client.support

public Class BasicAuthenticationInterceptor

extends Object
implements ClientHttpRequestInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.http.client.ClientHttpRequestInterceptor
Imports
java.io.IOException, java.nio.charset.Charset, org.springframework.http.HttpHeaders, .HttpRequest, org.springframework.http.client.ClientHttpRequestExecution, .ClientHttpRequestInterceptor, .ClientHttpResponse, org.springframework.lang.Nullable

ClientHttpRequestInterceptor to apply a given HTTP Basic Authentication username/password pair, unless a custom Authorization header has already been set.
Authors
Juergen Hoeller, Sam Brannen
Since
5.1.1
See Also
HttpHeaders#setBasicAuth, HttpHeaders#AUTHORIZATION

Field Summary

Modifier and TypeField and Description
private final String

Constructor Summary

AccessConstructor and Description
public
BasicAuthenticationInterceptor(String
the username to use
username
,
String
the password to use
password
)

Create a new interceptor which adds Basic Authentication for the given username and password.

public
BasicAuthenticationInterceptor(String
the username to use
username
,
String
the password to use
password
,
Charset
the charset to use
charset
)

Create a new interceptor which adds Basic Authentication for the given username and password, encoded using the specified charset.

Method Summary

Modifier and TypeMethod and Description
public ClientHttpResponse
intercept(HttpRequest
the request, containing method, URI, and headers
request
,
byte[]
the body of the request
body
,
ClientHttpRequestExecution
the request execution
execution
)

Implements org.springframework.http.client.ClientHttpRequestInterceptor.intercept.

Intercept the given request, and return a response.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

encodedCredentialsback to summary
private final String encodedCredentials

Constructor Detail

BasicAuthenticationInterceptorback to summary
public BasicAuthenticationInterceptor(String username, String password)

Create a new interceptor which adds Basic Authentication for the given username and password.

Parameters
username:String

the username to use

password:String

the password to use

See Also
HttpHeaders#setBasicAuth(String, String), HttpHeaders#encodeBasicAuth(String, String, Charset)
BasicAuthenticationInterceptorback to summary
public 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.

Parameters
username:String

the username to use

password:String

the password to use

charset:Charset

the charset to use

See Also
HttpHeaders#setBasicAuth(String, String, Charset), HttpHeaders#encodeBasicAuth(String, String, Charset)

Method Detail

interceptback to summary
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException

Implements org.springframework.http.client.ClientHttpRequestInterceptor.intercept.

Doc from org.springframework.http.client.ClientHttpRequestInterceptor.intercept.

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.
  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().

Parameters
request:HttpRequest

the request, containing method, URI, and headers

body:byte[]

the body of the request

execution:ClientHttpRequestExecution

the request execution

Returns:ClientHttpResponse

the response

Annotations
@Override
Exceptions
IOException:
in case of I/O errors