Class HTTPAuthenticationProvider
- Direct Known Subclasses:
DefaultHTTPAuthenticationProvider,ServletAuthenticationProvider
Implements the HTTP Authentication framework per RFC 9110 section 11.
RFC 9110 section 11.6.1: a 401 response MUST include a WWW-Authenticate
header with at least one applicable challenge. The generateChallenge()
method produces this header value.
This class provides the common authentication logic for various HTTP authentication schemes including:
- Basic - RFC 7617 username/password authentication
- Digest - RFC 7616 challenge-response authentication
- Bearer - RFC 6750 token-based authentication
- OAuth - RFC 6749 access token authentication
- JWT - JSON Web Token authentication
Concrete implementations must provide the authentication method, realm name, and credential verification logic by implementing the abstract methods.
Usage Example
public class MyAuthProvider extends HTTPAuthenticationProvider {
private final Realm realm;
protected String getAuthMethod() {
return HttpServletRequest.BASIC_AUTH;
}
protected String getRealmName() {
return "MyApp";
}
protected boolean passwordMatch(String realm, String username, String password) {
return this.realm.passwordMatch(username, password);
}
// ... other abstract method implementations
}
Thread Safety
This class is thread-safe. Nonce management uses concurrent data structures.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAuthentication result containing outcome and principal information. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionauthenticate(String authorizationHeader) Authenticates a request using the Authorization header value.final StringGenerates a WWW-Authenticate challenge header value for 401 responses.protected abstract StringGets the authentication method configured for this provider.protected abstract StringgetDigestHA1(String realm, String username) Gets the precomputed H(A1) hash for Digest authentication.protected abstract StringGets the realm name for this provider.Gets the set of authentication schemes supported by this provider.booleanChecks if authentication is required for requests to this provider.protected abstract booleanpasswordMatch(String realm, String username, String password) Verifies username and password credentials against the authentication realm.protected booleanChecks if the underlying Realm supports HTTP Digest authentication.final booleansupportsScheme(String scheme) Checks if this provider supports the given authentication scheme.protected abstract Realm.TokenValidationResultvalidateBearerToken(String token) Validates a Bearer token for token-based authentication.protected abstract Realm.TokenValidationResultvalidateOAuthToken(String accessToken) Validates an OAuth 2.0 access token.
-
Constructor Details
-
HTTPAuthenticationProvider
public HTTPAuthenticationProvider()
-
-
Method Details
-
getAuthMethod
Gets the authentication method configured for this provider.The return value should be one of the standard authentication method constants from
HttpServletRequestorHTTPAuthenticationMethods.- Returns:
- the authentication method (e.g., "BASIC", "DIGEST"), or null if none configured
-
getRealmName
Gets the realm name for this provider.The realm name is included in authentication challenges and is used to partition authentication spaces.
- Returns:
- the realm name, or null if none configured
-
passwordMatch
Verifies username and password credentials against the authentication realm.This method is called for Basic authentication and may also be used by other authentication mechanisms that require password verification.
- Parameters:
realm- the realm name for credential lookupusername- the username to verifypassword- the password to verify- Returns:
- true if the credentials are valid, false otherwise
-
getDigestHA1
Gets the precomputed H(A1) hash for Digest authentication.For Digest authentication, H(A1) = MD5(username:realm:password). Implementations may store this precomputed hash for security, avoiding the need to store plaintext passwords.
- Parameters:
realm- the realm nameusername- the username- Returns:
- the H(A1) hash as a lowercase hexadecimal string, or null if the user doesn't exist
-
validateBearerToken
Validates a Bearer token for token-based authentication.Called for Bearer authentication (RFC 6750). Implementations should verify the token's signature, expiration, and associated claims.
- Parameters:
token- the bearer token to validate- Returns:
- a
Realm.TokenValidationResultwith validation outcome, or null if Bearer authentication is not supported
-
validateOAuthToken
Validates an OAuth 2.0 access token.Called for OAuth authentication (RFC 6749). Implementations should verify the token against the authorization server or introspection endpoint.
- Parameters:
accessToken- the OAuth access token to validate- Returns:
- a
Realm.TokenValidationResultwith validation outcome, or null if OAuth authentication is not supported
-
supportsDigestAuth
protected boolean supportsDigestAuth()Checks if the underlying Realm supports HTTP Digest authentication.HTTP Digest authentication requires the Realm to provide the H(A1) hash via
getDigestHA1(String, String). Some Realm implementations (e.g., LDAP with hashed passwords) cannot support this.The default implementation returns true, assuming Digest is supported. Subclasses should override this if they can determine whether the Realm actually supports Digest authentication.
- Returns:
- true if Digest authentication is supported, false otherwise
-
authenticate
public final HTTPAuthenticationProvider.AuthenticationResult authenticate(String authorizationHeader) Authenticates a request using the Authorization header value.This method parses the Authorization header, determines the authentication scheme, and delegates to the appropriate authentication method based on the configured
getAuthMethod().- Parameters:
authorizationHeader- the Authorization header value from the HTTP request, in the format "Scheme credentials" (e.g., "Basic dXNlcjpwYXNz")- Returns:
- an
HTTPAuthenticationProvider.AuthenticationResultindicating success or failure with details
-
generateChallenge
Generates a WWW-Authenticate challenge header value for 401 responses.This method generates the appropriate challenge based on the configured authentication method. For Digest authentication, it includes a fresh nonce.
- Returns:
- the WWW-Authenticate header value (e.g., "Basic realm=\"MyApp\""), or null if no authentication is configured
-
supportsScheme
Checks if this provider supports the given authentication scheme.Scheme matching is case-insensitive per RFC 7235.
- Parameters:
scheme- the authentication scheme to check (e.g., "Basic", "Digest", "Bearer")- Returns:
- true if the scheme is supported by this provider, false otherwise
-
getSupportedSchemes
Gets the set of authentication schemes supported by this provider.- Returns:
- an unmodifiable set of supported scheme names (e.g., {"Basic"} or {"Bearer"})
-
isAuthenticationRequired
public boolean isAuthenticationRequired()Checks if authentication is required for requests to this provider.The default implementation returns true. Subclasses may override to implement optional authentication.
- Returns:
- true if authentication is required, false if authentication is optional
-