Class BearerTokenAuthenticationHandler
java.lang.Object
org.eclipse.rdf4j.http.client.spi.BearerTokenAuthenticationHandler
- All Implemented Interfaces:
AuthenticationHandler
AuthenticationHandler that adds an HTTP Bearer token to every request.
The Authorization: Bearer <token> header is added to every request passed to
authenticate(HttpRequest). The token can be supplied either as a static string or as a
Producer<String> that is invoked on each request, allowing dynamic tokens such as short-lived OAuth
access tokens to be refreshed transparently.
Example usage with a static token:
session.setAuthenticationHandler(new BearerTokenAuthenticationHandler("my-token"));
Example usage with a dynamic token producer:
session.setAuthenticationHandler(new BearerTokenAuthenticationHandler(tokenStore::currentToken));
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newBearerTokenAuthenticationHandlerfor the given static token.BearerTokenAuthenticationHandler(Producer<String> tokenProducer) Creates a newBearerTokenAuthenticationHandlerthat obtains the bearer token by invoking the givenProduceron each request. -
Method Summary
Modifier and TypeMethodDescriptionvoidauthenticate(HttpRequest request) Applies authentication credentials to the given request by modifying it in place.
-
Constructor Details
-
BearerTokenAuthenticationHandler
Creates a newBearerTokenAuthenticationHandlerfor the given static token.- Parameters:
token- the bearer token; must not benull
-
BearerTokenAuthenticationHandler
Creates a newBearerTokenAuthenticationHandlerthat obtains the bearer token by invoking the givenProduceron each request.Use this constructor when the token may change over time (e.g. a short-lived OAuth access token that is refreshed by the producer). The producer is called once per request; any checked exception it throws is wrapped in an
IllegalStateException.- Parameters:
tokenProducer- a producer that returns the current bearer token; must not benull
-
-
Method Details
-
authenticate
Description copied from interface:AuthenticationHandlerApplies authentication credentials to the given request by modifying it in place.- Specified by:
authenticatein interfaceAuthenticationHandler- Parameters:
request- the outgoing HTTP request to authenticate; nevernull
-