The @Secured annotation is a Spring Security annotation that restricts who may call a method, based on the caller's granted authorities. If the current user lacks every listed authority, Spring throws an AccessDeniedException before the method body runs.
It is the simplest form of method security: a plain list of role names, with no expression language. For conditions that depend on arguments or return values, @PreAuthorize is the usual choice.
@Secured lives in org.springframework.security.access.annotation. You place it on a method or class, and Spring Security wraps the bean in a proxy that checks the Authentication in the SecurityContext before delegating. The value is an array of authority names, and the user needs at least one of them.
Unlike hasRole("ADMIN") in expressions, @Secured does not add the ROLE_ prefix for you. Write ROLE_ADMIN exactly as the authority is stored.
Method security is off until you enable it. In current Spring Security, use @EnableMethodSecurity with securedEnabled = true:
@EnableMethodSecurity enables @PreAuthorize and @PostAuthorize by default; securedEnabled and jsr250Enabled are opt-in. The older @EnableGlobalMethodSecurity(securedEnabled = true) is deprecated, so migrate to the newer annotation when you upgrade.
Placing @Secured on a class applies it to every method of that class, and a method-level annotation overrides it:
If a user without the required authority calls closeAccount, the proxy throws AccessDeniedException. In a web application this becomes an HTTP 403 for authenticated users, or a redirect or 401 for anonymous ones, depending on your configuration.
#id == principal.id or reference method arguments.private method.@RolesAllowed (JSR-250) is portable but equally limited.| Annotation | Expressions | Prefix handling | Typical use |
|---|---|---|---|
@Secured | No | Full authority name, such as ROLE_ADMIN | Simple role checks |
@RolesAllowed | No | Adds ROLE_ automatically | Portable role checks |
@PreAuthorize | Yes (SpEL) | hasRole('ADMIN') adds ROLE_ | Argument-based and complex rules |
Prefer @PreAuthorize for new code because it covers every case @Secured handles, and it expresses ownership rules too. Keep @Secured where an existing codebase already uses it and rules are plain role lists.
Use Spring Security Test to run a call as a given user:
roles = "ADMIN" creates the authority ROLE_ADMIN, which matches @Secured("ROLE_ADMIN").
Method security is not enabled, or the call does not go through the proxy. Add @EnableMethodSecurity(securedEnabled = true) and make sure the call comes from another bean.
Yes. @Secured("ADMIN") checks for an authority literally named ADMIN, not ROLE_ADMIN.
No, it is still supported, but @PreAuthorize is more flexible. The older @EnableGlobalMethodSecurity that enabled it is the deprecated part.
Yes, on any Spring-managed bean method, including controller methods. For URL-level rules, authorizeHttpRequests in the security filter chain is usually clearer.
@Secured restricts method calls to users holding at least one listed authority. Enable it with @EnableMethodSecurity(securedEnabled = true), write full authority names such as ROLE_ADMIN, and remember it is proxy based and has no expression support. For anything more complex, use @PreAuthorize.