The @RolesAllowed annotation declares which security roles may call a class or method. It comes from the Jakarta Annotations specification (jakarta.annotation.security.RolesAllowed, formerly javax.annotation.security). In a JAX-RS (Jakarta REST) application it lets you protect resource methods declaratively: if the authenticated caller has none of the listed roles, the request is rejected before your code runs.
The annotation does nothing on its own. A runtime such as Jersey, RESTEasy or Spring Security has to enforce it, and the application must have authentication configured.
@RolesAllowed takes an array of role names. A caller is allowed if the container reports that the caller is in at least one of them, through SecurityContext.isUserInRole(role). It is part of a small family of annotations:
| Annotation | Meaning |
|---|---|
@RolesAllowed({"admin", "editor"}) | Only callers in one of these roles |
@PermitAll | Any caller, including anonymous ones |
@DenyAll | Nobody can call it |
Role names are plain strings and are case-sensitive. They must match the roles your identity provider or user store reports.
The class-level @RolesAllowed("user") applies to every method that has no annotation of its own. A method-level annotation overrides the class-level one, so delete requires admin and not user.
JAX-RS itself does not require implementations to enforce these annotations. You have to turn on enforcement.
Register RolesAllowedDynamicFeature:
The feature uses the SecurityContext of the request. If you authenticate with a token filter, that filter must set a SecurityContext whose isUserInRole reflects the token's roles.
On a full application server the container checks @RolesAllowed on EJBs and often on REST resources, using the roles from the login realm. Map the roles in web.xml or the server's deployment descriptor.
In Spring, enable JSR-250 support on method security:
Spring Security then reads @RolesAllowed, matching your roles against granted authorities. Spring Security adds the ROLE_ prefix to role names by default, so @RolesAllowed("admin") checks for the authority ROLE_admin. Spring-native code more often uses @PreAuthorize("hasRole('ADMIN')"), which is more expressive.
When you validate tokens yourself, a ContainerRequestFilter has to publish the roles:
The filter must run at Priorities.AUTHENTICATION, before the authorization filter that evaluates @RolesAllowed.
| Situation | Status |
|---|---|
| No authentication, role required | 403 in Jersey's default feature, 401 if your container or filter challenges |
| Authenticated but missing the role | 403 Forbidden |
| Role allowed | Method runs normally |
Do not rely on the annotation for everything. Check ownership of data (for example that a user may only read their own report) in your service code, because roles say what a caller can do, not which records they can touch.
@RolesAllowed is a standard annotation that only checks role names. @PreAuthorize is Spring-specific and accepts SpEL expressions, so it can check permissions, method arguments and ownership.
No. You must enable it, for example by registering RolesAllowedDynamicFeature in Jersey or setting jsr250Enabled = true in Spring, and you must have authentication that populates the security context.
Yes. Use @RolesAllowed({"admin", "manager"}). The caller needs any one of them, not all of them.
Since Jakarta EE 9 the package is jakarta.annotation.security. Older applications on Java EE 8 or earlier use javax.annotation.security. Do not mix the two in one application.
@RolesAllowed expresses role-based access control directly on JAX-RS classes and methods, with @PermitAll and @DenyAll as companions. Method-level annotations override class-level ones. It only works when authentication fills the SecurityContext and the runtime enforces the annotation, so register the feature for your framework and test both allowed and denied calls.