@PreAuthorize is a Spring Security annotation that checks a Spring Expression Language (SpEL) condition before a method runs. If the expression is true, the method executes. If it is false, Spring throws an AccessDeniedException and the method body never runs.
It is the main tool for method-level authorization in the service layer, where rules can use roles, authorities, method arguments and the current user. In Spring Security 6 and later you enable it with @EnableMethodSecurity, because @EnableGlobalMethodSecurity is deprecated.
Method security is off until you enable it. In Spring Security 6 and Spring Boot 3, add @EnableMethodSecurity to a configuration class. Its prePostEnabled option is already true by default.
Older code uses @EnableGlobalMethodSecurity(prePostEnabled = true). That annotation is deprecated since Spring Security 5.6 and replaced by @EnableMethodSecurity, so migrate when you upgrade.
The simplest use is checking what the current user is allowed to do.
hasRole('ADMIN') checks for the authority ROLE_ADMIN: Spring adds the ROLE_ prefix automatically. hasAuthority('product:write') compares the exact string, which is better for fine-grained permissions. Roles are really just authorities with a prefix.
SpEL can read method parameters by name (with the -parameters compiler flag or @P), the authentication object and the principal. This enables ownership rules.
The first rule lets users read only their own profile, with admins exempt. The second reads a property of the principal, which works when your UserDetails implementation has an id property. You can also call a bean in the expression:
Putting complex logic in a Spring bean keeps the annotation short and the logic unit-testable.
| Annotation | When it runs | Typical use |
|---|---|---|
@PreAuthorize | Before the method | Role, authority and argument checks |
@PostAuthorize | After the method, can use returnObject | Check the returned object, for example its owner |
@PreFilter | Before, filters a collection argument | Remove entries the user may not submit |
@PostFilter | After, filters a returned collection | Return only allowed entries |
@Secured | Before the method | Simple role list, no SpEL; needs securedEnabled = true |
@RolesAllowed | Before the method | Standard JSR 250 annotation; needs jsr250Enabled = true |
@PostFilter loads all data first and then filters in memory, so for large result sets prefer filtering in the database query.
A failed check throws org.springframework.security.access.AccessDeniedException. What the client sees depends on the layer above:
ExceptionTranslationFilter turns it into a 403 Forbidden for an authenticated user, or starts authentication (401 or a redirect) for an anonymous user.@ControllerAdvice you can handle AccessDeniedException yourself, but note that exceptions thrown from controller methods are handled by the advice before the filter sees them.The annotation does not return false or a 403 by itself; the response comes from the exception handling.
@Transactional, @PreAuthorize works through a proxy. A call from one method to another in the same class (self-invocation) bypasses the check.@WithMockUser or @WithUserDetails from Spring Security Test.hasRole('ADMIN') looks for ROLE_ADMIN and adds the prefix for you, while hasAuthority('ADMIN') looks for exactly ADMIN. Use roles for coarse groups and authorities for specific permissions.
Prefer @PreAuthorize. It supports SpEL, so it can express argument and ownership rules, which @Secured cannot.
Yes, on any Spring bean method, including controller methods. Many teams still keep the rules in the service layer so they apply to every caller.
Enable method security in a Spring test slice and use @WithMockUser(roles = "ADMIN"). Assert that an unauthorized call throws AccessDeniedException.
@PreAuthorize evaluates a SpEL expression before a method runs and blocks the call when it fails. Enable it with @EnableMethodSecurity, use hasRole and hasAuthority for static rules, use arguments and the principal for ownership rules, and remember it works only through the Spring proxy.