Method-level security in Spring Security restricts access to individual methods, usually service methods, using annotations such as @PreAuthorize, @PostAuthorize, @Secured and @RolesAllowed. Rules are evaluated through a proxy before or after the method runs, so they protect business logic regardless of which controller, scheduled job or message listener calls it.
In current Spring Security (6.x with Spring Boot 3), you enable it with @EnableMethodSecurity. The older @EnableGlobalMethodSecurity is deprecated and was replaced.
@EnableMethodSecurity@PreAuthorize and @PostAuthorize@Secured and @RolesAllowed@PreFilter and @PostFilter@EnableMethodSecurityAdd the annotation to a configuration class. In Spring Security 6, @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter are enabled by default. @Secured and JSR-250 annotations must be switched on explicitly.
| Attribute | Default | Enables |
|---|---|---|
prePostEnabled | true | @PreAuthorize, @PostAuthorize, @PreFilter, @PostFilter |
securedEnabled | false | @Secured |
jsr250Enabled | false | @RolesAllowed, @PermitAll, @DenyAll |
Migration note: @EnableGlobalMethodSecurity(prePostEnabled = true) from Spring Security 5 is deprecated. Replace it with @EnableMethodSecurity, which also uses the newer authorization manager API.
@PreAuthorize and @PostAuthorize@PreAuthorize runs a Spring Expression Language (SpEL) check before the method body. It is the most flexible option and the one to use by default.
Notes on the expressions:
hasRole('ADMIN') checks for the authority ROLE_ADMIN; the ROLE_ prefix is added for you.#doc refers to the method argument named doc.authentication and principal are available in every expression.@PostAuthorize can inspect returnObject, but the method has already run, so do not use it to guard side effects.@Secured and @RolesAllowedThese two annotations only check roles and accept no expressions.
@Secured requires the full ROLE_ prefix. @RolesAllowed comes from Jakarta Annotations (package jakarta.annotation.security since Jakarta EE 9) and takes role names without the prefix. For new code, prefer @PreAuthorize, since it covers everything these do and more.
@PreFilter and @PostFilterInstead of rejecting the call, these annotations remove elements the user is not allowed to see.
This loads every row before filtering, so for large tables apply the restriction in the query instead.
Repeating long expressions invites mistakes. Wrap them in a meta-annotation or a bean method.
Now @IsAdmin can be placed on any method. For domain rules, call a bean from the expression, for example @PreAuthorize("@documentPolicy.canEdit(#id, authentication)"), which keeps logic testable.
SecurityFilterChain; it does not replace them.@WithMockUser(roles = "ADMIN") from spring-security-test and assert that unauthorised calls throw AccessDeniedException.@PreAuthorize and @Secured?@PreAuthorize accepts SpEL expressions, so it can test authorities, arguments and ownership. @Secured only checks a list of role names.
@EnableGlobalMethodSecurity still supported?It is deprecated. Use @EnableMethodSecurity, which is the recommended replacement from Spring Security 5.6 and the default model in 6.x.
@PreAuthorize ignored?Common causes are a missing @EnableMethodSecurity, a self-invocation inside the same class, or a method that is private or on an object not created by Spring.
Use both. URL rules give a coarse first barrier, while method security protects the business logic wherever it is called from.
Enable method security with @EnableMethodSecurity, put @PreAuthorize on service methods for expression-based rules, and use @PostAuthorize, @PostFilter or @Secured only where they fit. Remember that the checks run through proxies, keep expressions short by using meta-annotations or policy beans, and test the denied path as carefully as the allowed one.