The @PreAuthorize annotation checks an authorization rule before a method runs, and Spring Security throws an AccessDeniedException if the rule is false. You write the rule as a Spring Expression Language (SpEL) string, so you can test roles, permissions, method arguments and the current user in one place.
It is part of method security, which protects your service layer regardless of how a request reached it. This guide uses Spring Security 6 and later with Spring Boot 3, where @EnableMethodSecurity replaces the deprecated @EnableGlobalMethodSecurity.
@PreAuthorize does nothing until method security is switched on. Add @EnableMethodSecurity to a configuration class:
In Spring Security 6, prePostEnabled is true by default, so @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter all work. The older @EnableGlobalMethodSecurity(prePostEnabled = true) is deprecated.
Place the annotation on a service method or on a class to cover all its methods:
hasRole('ADMIN') automatically adds the ROLE_ prefix, so it matches the authority ROLE_ADMIN. hasAuthority('report:read') matches the string exactly. If the check fails, Spring Security raises AccessDeniedException, which the web layer turns into HTTP 403 for an authenticated user or a redirect or 401 for an anonymous one.
SpEL can read method parameters with #name and the principal through authentication:
The first rule lets users read only their own profile unless they are administrators. The second delegates to a Spring bean named documentPermissions, which keeps complex rules testable in plain Java. Parameter names resolve when the code is compiled with -parameters, which Spring Boot's Maven and Gradle plugins enable by default; otherwise use @P("name").
| Annotation | When it runs | Typical use |
|---|---|---|
@PreAuthorize | Before the method | Role, permission or argument checks |
@PostAuthorize | After the method, can use returnObject | Check ownership of a loaded entity |
@PreFilter / @PostFilter | Filters collection arguments or results | Per-element visibility |
@Secured | Before the method | Simple role names only, no SpEL |
@RolesAllowed | Before the method | Standard JSR-250 role check |
@PreAuthorize is the most flexible and is the recommended choice for new code.
new has no proxy.hasRole('ROLE_ADMIN') is accepted in recent versions, but prefer hasRole('ADMIN') for clarity.authorizeHttpRequests for coarse access and @PreAuthorize for fine-grained, domain-level rules.@WithMockUser(roles = "ADMIN") from spring-security-test to assert both allowed and denied cases.Spring Security throws AccessDeniedException before the method body runs. In a web application the default handling returns 403, and you can customise it with an AccessDeniedHandler.
Yes. Spring Boot does not enable method security automatically, so add @EnableMethodSecurity to a configuration class.
Yes, it works on any Spring bean method, including controllers. Putting it on the service layer protects the logic regardless of the caller.
hasRole('X') checks for the authority ROLE_X, while hasAuthority('X') checks the exact string.
@PreAuthorize runs a SpEL rule before a method and blocks the call with AccessDeniedException when it fails. Enable it with @EnableMethodSecurity, express rules with roles, authorities, arguments and bean calls, and remember the proxy limitation on self-invocation. Use it together with request-level rules for defence in depth.