In Spring Security, @PreAuthorize secures a method by evaluating a SpEL expression before the method is invoked. If the expression returns false, the call is rejected with an AccessDeniedException and the method body never executes. It is the standard way to apply role-based and permission-based access control at the service layer.
This answer covers Spring Security 6 and later on Spring Boot 3, including how to wire it up, test it and build reusable rules.
Spring Security offers two layers of authorization. Request authorization (authorizeHttpRequests) matches URLs. Method security matches Java methods through AOP proxies, so the rule travels with the code and applies whether the call comes from a controller, a scheduled job or a message listener.
@PreAuthorize is the "before" interceptor of method security. Its companions are @PostAuthorize (after the call), @PreFilter and @PostFilter (collection filtering). Enable them all with one annotation:
@EnableMethodSecurity replaces the deprecated @EnableGlobalMethodSecurity. Pre and post annotations are on by default; @Secured and JSR-250 @RolesAllowed need securedEnabled = true or jsr250Enabled = true.
The expression has access to built-in functions and variables:
| Expression | Meaning |
|---|---|
hasRole('ADMIN') | Authority ROLE_ADMIN is present |
hasAnyAuthority('read', 'write') | At least one authority matches |
isAuthenticated() | User is logged in, not anonymous |
permitAll() / denyAll() | Always allow or deny |
#id | Value of the method parameter id |
authentication.name | Current username |
principal | The authenticated principal object |
@bean.method(...) | Call a Spring bean |
Expressions run on the invocation thread with the SecurityContext of the caller, so asynchronous methods need the context propagated (for example with DelegatingSecurityContextExecutor).
Long expressions get hard to read. Move logic into a bean and call it by name:
You can also create a meta-annotation so the rule has a name:
Annotate methods with @IsAdmin and change the rule in one place.
Use spring-security-test to run the method as a chosen user:
Always test the denied case as well as the allowed one, because a missing @EnableMethodSecurity makes every annotation silently inactive.
this.method()) skip it.final or private methods cannot be proxied with CGLIB.@PreAuthorize runs before the method, so it cannot see the returned value; use @PostAuthorize("returnObject.owner == authentication.name") for that.@Secured accepts only role names, while @PreAuthorize accepts full SpEL, including arguments and beans. Prefer @PreAuthorize.
Usually method security is not enabled, the bean is not Spring-managed, or the method is called from within the same class. Check @EnableMethodSecurity first.
Yes. Scopes appear as authorities with the SCOPE_ prefix, so use hasAuthority('SCOPE_orders.read').
Use both. URL rules give a coarse first barrier, and @PreAuthorize protects business methods.
@PreAuthorize evaluates a SpEL expression before a Spring Security-protected method runs and throws AccessDeniedException when it is false. Enable it with @EnableMethodSecurity, keep complex rules in beans or meta-annotations, test denied paths and avoid self-invocation.