Class ApiKeyGuard
java.lang.Object
org.apache.juneau.rest.server.guard.RestGuard
org.apache.juneau.rest.server.auth.ApiKeyGuard
AuthN
RestGuard that authenticates requests via a raw API-key string.
On every request, the guard:
- Extracts the key from the configured source — header (default
X-API-Key), query parameter, or cookie. - Delegates lookup to the configured
ApiKeyStore. - On success, stashes the returned
Principalon the request attributes under"juneau.principal". - On any failure path (missing key, unknown key), throws
AuthenticationExceptionwith aWWW-Authenticate: ApiKey realm="<realm>"response header.
Usage
Security notes
- API keys are credentials; never log them, always pair with TLS.
- Query-string keys leak into proxy / access logs and browser history — prefer header or cookie sources for production traffic. Query-string is offered for legacy / debugging use only.
ApiKeyStoreimplementations should compare keys in constant time (MessageDigest.isEqual(byte[], byte[])).
See Also:
- Since:
- 10.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static enumSource of the API-key string. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedApiKeyGuard(ApiKeyGuard.Builder builder) Constructor.ApiKeyGuard(ApiKeyStore store) Convenience constructor: header source with default name"X-API-Key"and default realm. -
Method Summary
Modifier and TypeMethodDescriptionstatic ApiKeyGuard.Buildercreate()Static creator.booleanguard(RestRequest req, RestResponse res) Checks the current HTTP request and throws aBasicHttpExceptionif the guard does not permit the request.booleanReturnstrue if the specified request can pass through this guard.
-
Constructor Details
-
ApiKeyGuard
Constructor.- Parameters:
builder- The builder to read configuration from. Must not benull .
-
ApiKeyGuard
Convenience constructor: header source with default name"X-API-Key"and default realm.- Parameters:
store- The API-key store. Must not benull .
-
-
Method Details
-
create
Static creator.- Returns:
- A new builder.
-
guard
Description copied from class:RestGuardChecks the current HTTP request and throws aBasicHttpExceptionif the guard does not permit the request.By default, throws an
SC_FORBIDDEN exception ifRestGuard.isRequestAllowed(RestRequest)returnsfalse .Subclasses are free to override this method to tailor the behavior of how to handle unauthorized requests.
-
isRequestAllowed
Description copied from class:RestGuardReturnstrue if the specified request can pass through this guard.- Specified by:
isRequestAllowedin classRestGuard- Parameters:
req- The servlet request.- Returns:
true if the specified request can pass through this guard.
-