@RolesAllowed is the standard Jakarta annotation (jakarta.annotation.security.RolesAllowed, formerly javax.annotation.security) that restricts a method or class to users holding at least one of the listed roles. In Spring Security you enable it with @EnableMethodSecurity(jsr250Enabled = true), and a call from a user without a matching role fails with an AccessDeniedException.
It is a simple, role-only alternative to Spring's own @PreAuthorize. Use it when you want portable, annotation-based role checks and do not need expressions.
JSR-250 annotations are off by default. Turn them on with @EnableMethodSecurity, which replaces the deprecated @EnableGlobalMethodSecurity in Spring Security 6 and later.
By default @EnableMethodSecurity enables @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter. The jsr250Enabled flag adds support for @RolesAllowed, @PermitAll and @DenyAll. Method security works through proxies, so it applies to calls made through the Spring bean, not to calls from within the same class.
You also need the Jakarta annotations API on the classpath. Spring Boot starters normally provide it, and @RolesAllowed lives in jakarta.annotation-api.
How the rules combine:
@RolesAllowed("USER") applies to every method of the class.viewAuditLog needs ADMIN or AUDITOR, not USER.@PermitAll allows everyone who reaches the method, and @DenyAll blocks it for everyone.Spring Security stores granted authorities as strings. For @RolesAllowed("ADMIN"), Spring checks for the authority ROLE_ADMIN, because it adds the ROLE_ prefix to the role name for you.
If your authorities do not start with ROLE_ (for example SCOPE_read from an OAuth 2 token or custom names like report:view), @RolesAllowed will not match them, since it always looks for the prefixed form. In that case, use @PreAuthorize("hasAuthority('report:view')") instead.
| Annotation | Standard | Expressions | Typical use |
|---|---|---|---|
@RolesAllowed | Jakarta (JSR-250) | No | Simple, portable role checks |
@Secured | Spring | No | Simple role checks, Spring only |
@PreAuthorize | Spring | Yes (SpEL) | Authorities, parameters, ownership rules |
@PreAuthorize is the most flexible: it can check method arguments, such as @PreAuthorize("#userId == authentication.name"), or combine rules. @RolesAllowed is a good fit when roles are all you need and you want annotations that are not tied to Spring.
A failed check throws AccessDeniedException. For web requests, Spring Security's exception translation turns it into a 403 response (or a 401 for unauthenticated users). In a REST controller, you can customise the response:
Test the rules with @WithMockUser(roles = "ADMIN") from spring-security-test, and write a test for the denied case too, not only the allowed one.
jsr250Enabled = true, so the annotation is silently ignored and the method is open to everyone.@RolesAllowed("ROLE_ADMIN") while authorities are already ROLE_ADMIN. The role name should be ADMIN.@EnableGlobalMethodSecurity(jsr250Enabled = true), which is deprecated.@RolesAllowed a Spring annotation?No. It is a Jakarta Common Annotations (formerly JSR-250) annotation. Spring Security supports it once jsr250Enabled is set to true.
ROLE_ADMIN or ADMIN?Write ADMIN. Spring adds the ROLE_ prefix when checking the granted authorities.
@RolesAllowed require two roles at once?No. Multiple values mean any one is sufficient. For AND logic use @PreAuthorize("hasRole('A') and hasRole('B')").
Most often method security is not enabled, or the call goes through this inside the same class, bypassing the proxy.
Enable JSR-250 support with @EnableMethodSecurity(jsr250Enabled = true), then annotate classes or methods with @RolesAllowed. Remember the implicit ROLE_ prefix, that listed roles are alternatives, and that method annotations override class annotations. Reach for @PreAuthorize when you need authorities, arguments or combined conditions.