Class RestGuard
- Direct Known Subclasses:
AdminGuard
,RoleBasedRestGuard
Description
Implements a guard mechanism for REST method calls that allows requests to be rejected before invocation of the REST method. For example, guards can be used to ensure that only administrators can call certain methods.
Guards are applied to REST methods declaratively through the @Rest(guards)
or
@RestOp(guards)
annotations.
If multiple guards are specified, ALL guards must pass in order for the request to proceed.
How to implement
Typically, guards will be used for permissions checking on the user making the request, but it can also be used for other purposes like pre-call validation of a request.
Implementers should simply throw a BasicHttpException
from the guard(RestRequest, RestResponse)
method to abort processing on the current request.
Guards must implement a no-args constructor.
Example usage:
Example implementation:
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
guard
(RestRequest req, RestResponse res) Checks the current HTTP request and throws aBasicHttpException
if the guard does not permit the request.abstract boolean
Returnstrue if the specified request can pass through this guard.
-
Constructor Details
-
RestGuard
public RestGuard()
-
-
Method Details
-
guard
Checks the current HTTP request and throws aBasicHttpException
if the guard does not permit the request.By default, throws an
SC_FORBIDDEN exception ifisRequestAllowed(RestRequest)
returnsfalse .Subclasses are free to override this method to tailor the behavior of how to handle unauthorized requests.
- 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
Returnstrue if the specified request can pass through this guard.- Parameters:
req
- The servlet request.- Returns:
true if the specified request can pass through this guard.
-