Class BearerTokenGuard
java.lang.Object
org.apache.juneau.rest.server.guard.RestGuard
org.apache.juneau.rest.server.auth.BearerTokenGuard
AuthN
RestGuard that authenticates requests via the
RFC 6750 Bearer Token scheme.
On every request, the guard:
- Reads the
Authorizationrequest header. - Confirms it starts with
"Bearer "(case-insensitive) followed by a non-blank token. - Delegates the raw token to the configured
TokenValidator. - On success, stashes the returned
Principalon the request attributes under"juneau.principal"so downstream handlers can read it via@Auth Principalorreq.getAttributes().get(PRINCIPAL_ATTR). - On any failure path (missing header, malformed scheme, validator throws), throws
AuthenticationExceptionwith aWWW-Authenticate: Bearer realm="<realm>"response header set per RFC 7235 §4.1.
Usage
Security notes
- Bearer tokens are credentials — never log the full token, never echo it in a response body, and always pair with TLS in production.
TokenValidators should run in constant time when possible (in particular, opaque-token lookups should use constant-time comparison to defeat timing side-channels).- For JWT verification, use the
JwtTokenValidatorshipped in the optionaljuneau-rest-server-auth-jwtsub-module — it enforces algorithm allowlisting and mandatoryiss/aud/exp/nbfchecks by default.
See Also:
- Since:
- 10.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBearerTokenGuard(BearerTokenGuard.Builder builder) Constructor.BearerTokenGuard(TokenValidator validator) Convenience constructor that uses the default realm ("api"). -
Method Summary
Modifier and TypeMethodDescriptionstatic BearerTokenGuard.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
-
BearerTokenGuard
Constructor.- Parameters:
builder- The builder to read configuration from. Must not benull .
-
BearerTokenGuard
Convenience constructor that uses the default realm ("api").- Parameters:
validator- The token validator. 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.
-