The @Secured annotation restricts who can call a method in a Spring application. You list one or more authority names, and Spring Security checks them before the method runs. If the current user has none of them, the call is rejected with an AccessDeniedException (or an AuthenticationException for anonymous users) and the method body never executes.
It is the simplest of Spring Security's method security annotations, but it is also the least flexible. It supports plain authority names only, with no expressions, so many projects use @PreAuthorize instead.
Method security in Spring Security is applied through AOP. When method security is enabled, Spring wraps the bean in a proxy. Each call passes through an interceptor that reads the Authentication from the SecurityContext and compares its granted authorities with the values in the annotation. If any one matches, the call proceeds.
The values in the array are combined with OR: a user needs at least one of them. You can also put @Secured on a class to protect all its methods, and a method-level annotation overrides the class-level one.
@Secured is off by default. Turn it on with @EnableMethodSecurity(securedEnabled = true) on a configuration class.
Notes on versions:
@EnableMethodSecurity replaced @EnableGlobalMethodSecurity, which is deprecated. The new annotation also turns @PreAuthorize and @PostAuthorize on by default, while securedEnabled and jsr250Enabled stay false until you set them.jakarta namespace. WebSecurityConfigurerAdapter was removed, so security is configured with a SecurityFilterChain bean as shown above.@Secured compares the string you write with the user's granted authorities exactly. Roles are stored with the ROLE_ prefix, so @Secured("ADMIN") does not match a user whose authority is ROLE_ADMIN. Write @Secured("ROLE_ADMIN").
You can use it with any authority string, not only roles, for example @Secured("SCOPE_invoices.read") for OAuth 2 scopes. For checks such as "any authenticated user", prefer @PreAuthorize("isAuthenticated()").
Use spring-security-test to run a method as a given user.
@WithMockUser(roles = "ADMIN") creates the authority ROLE_ADMIN automatically.
| Feature | @Secured | @PreAuthorize | @RolesAllowed |
|---|---|---|---|
| Origin | Spring Security | Spring Security | JSR-250 (Jakarta) |
Needs ROLE_ prefix in the value | Yes | Only inside hasAuthority; hasRole adds it | No, added for you |
| SpEL expressions | No | Yes | No |
| Access to method arguments or return value | No | Yes | No |
| Enabled by | securedEnabled = true | On by default | jsr250Enabled = true |
If you need a rule such as "the user may edit only their own documents", use @PreAuthorize("#owner == authentication.name"). If the rule is just a list of roles, @Secured is fine and easy to read.
SecurityFilterChain as well.An AccessDeniedException for an authenticated user without the required authority. In Spring Security 6.3 and later this is an AuthorizationDeniedException, a subclass of it. Anonymous users trigger the authentication entry point instead.
No. It accepts only plain strings. Use @PreAuthorize when you need expressions.
Usually because method security is not enabled. Add @EnableMethodSecurity(securedEnabled = true), and make sure the call goes through the Spring proxy rather than a direct call inside the same class.
It works and is supported. Many teams choose @PreAuthorize for consistency, because it handles both simple role checks and complex rules.
@Secured guards methods by requiring one of the listed authorities, using the exact names including the ROLE_ prefix. Enable it with @EnableMethodSecurity(securedEnabled = true), test it with @WithMockUser, and switch to @PreAuthorize when you need expressions or access to arguments.