Class ConfigApiKeyGuard
LoadConfigResource with a shared API key.
The expected key is read from the
Clients supply the key in either an MessageDigest.isEqual(byte[], byte[]) over UTF-8 bytes).
The key is read once, when the guard is created, and the Config used to read it is closed straight away.
Changing the key requires a restart.
Requests that did not arrive over a secure channel are refused unless they come from a loopback address or
The guard fails closed: if no key is configured (missing, empty or unreadable setting), every request is rejected with a 403. The key is never logged or echoed. Always use TLS.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringReturns the key read at startup, ornull if none is configured or the configuration could not be read.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
-
ConfigApiKeyGuard
public ConfigApiKeyGuard()Constructor. Reads the settings once and releases the configuration.
-
-
Method Details
-
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.
- Overrides:
guardin classRestGuard- Parameters:
req- The servlet request.res- The servlet response.- Returns:
true if request can proceed. Specifyfalse if you're doing something like a redirection to a login page.- Throws:
BasicHttpException- Thrown to abort processing on current request.
-
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.
-
configuredKey
Returns the key read at startup, ornull if none is configured or the configuration could not be read.- Returns:
- The configured key, or
null .
-