@PreAuthorize is a Spring Security annotation that checks an access rule before a method runs. The rule is a Spring Expression Language (SpEL) string such as hasRole('ADMIN'). If the expression evaluates to false, Spring throws an AccessDeniedException and the method body is never executed.
It gives you method-level, fine-grained authorization on service or controller methods, in addition to URL rules in the security filter chain.
@PreAuthorize does nothing until method security is on. In Spring Security 6 (Spring Boot 3 and later), use @EnableMethodSecurity. Pre/post annotations are enabled by default:
The older @EnableGlobalMethodSecurity(prePostEnabled = true) is deprecated and was replaced by @EnableMethodSecurity, so use the new annotation in new code.
hasRole('ADMIN') automatically adds the ROLE_ prefix, so it matches the authority ROLE_ADMIN. hasAuthority('ADMIN') matches the exact string with no prefix. Mixing the two up is the most common cause of unexpected 403 responses.
| Expression | Meaning |
|---|---|
hasRole('X') / hasAnyRole(...) | User has role X (prefix ROLE_ is added) |
hasAuthority('x') / hasAnyAuthority(...) | User has the exact authority |
isAuthenticated() | User is logged in |
permitAll() / denyAll() | Always allow or always deny |
authentication.name | Name of the current user |
principal | The authenticated principal object |
#param | A method argument, by parameter name |
@beanName.method(...) | Calls a Spring bean for custom logic |
Expressions can be combined with and, or and !.
You can compare arguments with the current user, which is how you enforce that people only see their own data:
Argument names are resolved from compiled parameter names, so compile with the -parameters flag. Spring Boot's Maven and Gradle plugins configure this by default. Alternatively, mark an argument with @P("username").
For rules that need a database lookup, delegate to a bean:
This keeps complex logic in plain Java where you can unit test it.
Method security is applied through a proxy. A call from outside the bean goes through the proxy, which evaluates the expression first. Two consequences follow:
@PreAuthorize method of the same class, the proxy is bypassed.final classes and methods, and place annotations on methods of Spring-managed beans.A denied call throws AccessDeniedException. In a web application, ExceptionTranslationFilter converts it to a 403 Forbidden response, or to a login redirect or 401 when the user is not authenticated. If you need a custom JSON error body, handle it with an AccessDeniedHandler or a @ControllerAdvice. Note that a @ControllerAdvice exception handler for AccessDeniedException only catches exceptions thrown from controller methods.
@PostAuthorize runs after the method and can inspect returnObject, for example @PostAuthorize("returnObject.owner == authentication.name").@PreFilter and @PostFilter filter collection arguments or results. @PostFilter loads everything first, so avoid it on large data sets and filter in the query instead.@Secured and JSR-250 @RolesAllowed are simpler role-only alternatives with no SpEL, enabled through @EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true).URL rules in authorizeHttpRequests protect web endpoints and are the first line of defence. @PreAuthorize protects the code itself, so the rule still applies when the method is called from a scheduled job, a message listener or another service. Use both for defence in depth, and keep business-specific rules on the method.
Method security is probably not enabled. Add @EnableMethodSecurity, and check that the call goes through the Spring proxy rather than being a self-invocation.
hasRole('ADMIN') checks for ROLE_ADMIN, because it adds the prefix. hasAuthority('ADMIN') checks the exact name.
Yes. A class-level annotation applies to all its methods, and a method-level annotation overrides it.
Yes, with @EnableReactiveMethodSecurity, and the annotated methods must return Mono or Flux.
@PreAuthorize evaluates a SpEL rule before a method runs and blocks the call with an AccessDeniedException when the rule fails. Enable it with @EnableMethodSecurity, use hasRole and hasAuthority carefully, move complex checks into beans, and remember that proxies do not intercept self-invocation.