@Secured is a Spring Security annotation that restricts a method to users who hold at least one of the listed roles or authorities. If the caller has none of them, Spring throws an AccessDeniedException before the method body runs. It is disabled by default and must be switched on with @EnableMethodSecurity(securedEnabled = true).
@Secured is simple but limited: it accepts only role names, not expressions. For anything more expressive, Spring Security recommends @PreAuthorize.
In Spring Security 5.6 and later, including Spring Security 6 and 7, method security is configured with @EnableMethodSecurity. The older @EnableGlobalMethodSecurity is deprecated.
@EnableMethodSecurity turns on @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter by default. securedEnabled = true adds @Secured, and jsr250Enabled = true adds @RolesAllowed.
With an array, access is granted when the user has any one of the listed roles. You can also place @Secured on a class to apply it to every method, and a method-level annotation overrides the class-level one.
@Secured does not add a prefix. The value you write is compared directly with the user's granted authorities, so @Secured("ADMIN") will not match a user who has ROLE_ADMIN. Write @Secured("ROLE_ADMIN").
Method security uses a Spring AOP proxy. When the check fails, the proxy throws org.springframework.security.access.AccessDeniedException. In a web application, ExceptionTranslationFilter turns it into an HTTP 403 for authenticated users, or sends unauthenticated users to the login entry point (HTTP 401 for APIs).
You can verify this behaviour with a test:
@WithMockUser(roles = "ADMIN") creates the authority ROLE_ADMIN for you.
| Feature | @Secured | @PreAuthorize | @RolesAllowed |
|---|---|---|---|
| Package | org.springframework.security.access.annotation | org.springframework.security.access.prepost | jakarta.annotation.security |
| Enabled by default | No | Yes, with @EnableMethodSecurity | No |
| Expression language (SpEL) | No | Yes | No |
| Role prefix | Write ROLE_ yourself | hasRole('ADMIN') adds it | Added automatically |
| Method arguments in rules | No | Yes | No |
Example of something @Secured cannot express:
Choose @PreAuthorize for new code unless you specifically need the simplicity or portability of the other annotations. Mixing them in one codebase is possible but harder to audit.
final classes or methods cannot be proxied by CGLIB.authorizeHttpRequests in the security filter chain as the first line of defence and use method security as a second layer in the service tier.securedEnabled = true means the annotation is silently ignored.Add @EnableMethodSecurity(securedEnabled = true) to a configuration class. In older code you may see @EnableGlobalMethodSecurity(securedEnabled = true), which is deprecated.
@Secured compares the value with the raw authority name. Use @Secured("ROLE_ADMIN"), or switch to @PreAuthorize("hasRole('ADMIN')"), which adds the prefix.
No. It supports only a list of role or authority names. Use @PreAuthorize or @PostAuthorize for expressions.
No, it is still supported, but @PreAuthorize is the recommended choice for new applications.
@Secured guards a method with a fixed list of roles and throws AccessDeniedException when the caller lacks them. Enable it with @EnableMethodSecurity(securedEnabled = true), include the ROLE_ prefix, and prefer @PreAuthorize when you need expressions or parameter-based rules.