Class BearerTokenAuthenticationHandler

java.lang.Object
org.eclipse.rdf4j.http.client.spi.BearerTokenAuthenticationHandler
All Implemented Interfaces:
AuthenticationHandler

public class BearerTokenAuthenticationHandler extends Object implements 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 Details

    • BearerTokenAuthenticationHandler

      public BearerTokenAuthenticationHandler(String token)
      Creates a new BearerTokenAuthenticationHandler for the given static token.
      Parameters:
      token - the bearer token; must not be null
    • BearerTokenAuthenticationHandler

      public BearerTokenAuthenticationHandler(Producer<String> tokenProducer)
      Creates a new BearerTokenAuthenticationHandler that obtains the bearer token by invoking the given Producer on 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 be null
  • Method Details

    • authenticate

      public void authenticate(HttpRequest request)
      Description copied from interface: AuthenticationHandler
      Applies authentication credentials to the given request by modifying it in place.
      Specified by:
      authenticate in interface AuthenticationHandler
      Parameters:
      request - the outgoing HTTP request to authenticate; never null