SecurityExpressionRoot is the root object of every Spring Security expression. When you write @PreAuthorize("hasRole('ADMIN')"), Spring evaluates the SpEL string against an instance of a SecurityExpressionRoot subclass, and that is where hasRole, hasAuthority, isAuthenticated, permitAll and similar methods come from.
It also exposes the current Authentication and its principal, so expressions can read authentication.name or principal.username. Extending it is the classic way to add your own expression methods.
SecurityExpressionRoot is an abstract class in org.springframework.security.access.expression. It implements SecurityExpressionOperations and holds the Authentication used for the check. Specialised subclasses add context:
MethodSecurityExpressionRoot is used for method security (@PreAuthorize, @PostAuthorize, @PreFilter, @PostFilter). It adds filterObject, returnObject and this.WebSecurityExpressionRoot is used for URL authorization expressions. It adds the current request, for example for hasIpAddress(...).The built-in expression members are:
| Expression | Meaning |
|---|---|
hasRole('ADMIN'), hasAnyRole(...) | Checks a role. The ROLE_ prefix is added automatically. |
hasAuthority('orders:write'), hasAnyAuthority(...) | Checks an authority exactly as written, without a prefix. |
permitAll, denyAll | Always true or always false. |
isAnonymous(), isAuthenticated() | Anonymous or logged-in user. |
isFullyAuthenticated() | Logged in, and not by a remember-me cookie. |
isRememberMe() | Authenticated through remember-me. |
hasPermission(target, 'read') | Delegates to the configured PermissionEvaluator. |
authentication, principal | The Authentication object and its principal. |
For a method annotated with @PreAuthorize, the flow is:
MethodSecurityExpressionHandler builds an evaluation context. Its root object is a MethodSecurityExpressionRoot holding the current Authentication.#name, so you can write #id or #order.owner.AccessDeniedException.Here hasRole('ADMIN') is a method on the root object, #username is the method argument and authentication.name reads the root's authentication property.
In Spring Security 6 and later, use @EnableMethodSecurity. It replaces @EnableGlobalMethodSecurity, which is deprecated. @PreAuthorize and @PostAuthorize are on by default.
One correction to a common misconception: @Secured does not use SpEL. It takes a plain list of role names (@Secured("ROLE_ADMIN")), so it has no access to SecurityExpressionRoot. To use expressions, use @PreAuthorize or @PostAuthorize. @Secured must also be enabled with @EnableMethodSecurity(securedEnabled = true).
Often you do not need to extend the root at all. SpEL can call any Spring bean with the @ prefix. This is the simplest and most testable way to add custom rules.
The custom class is plain Java, so you can unit test it without a Spring Security context.
When you want a domain method directly inside the expression, such as isTenantAdmin(), you can supply your own root. In Spring Security 6 this is done by overriding createEvaluationContext in a MethodSecurityExpressionHandler, and wrapping the default root so the standard methods still work.
TenantExpressionRoot implements MethodSecurityExpressionOperations, forwards every standard method to the delegate, and adds its own methods:
You can then write @PreAuthorize("isTenantAdmin()"). The handler bean must be static and marked as infrastructure so it is created early enough, which is why the example uses static and @Role. Classes like this are an advanced extension point, so check the Spring Security reference for your version before relying on this exact shape.
hasRole('ROLE_ADMIN'). The prefix is added for you, so use hasRole('ADMIN'), or hasAuthority('ROLE_ADMIN').hasRole('ADMIN') checks for the authority ROLE_ADMIN, because it adds the prefix. hasAuthority('ADMIN') checks the exact string you give.
No. @Secured takes role names and does not evaluate SpEL. Use @PreAuthorize for expressions.
No. Calling a bean with @beanName.method(...) inside the expression is simpler and easier to test.
It is a property of the root object, so authentication and principal resolve against the current Authentication held by SecurityExpressionRoot.
SecurityExpressionRoot is the object behind every Spring Security expression: it supplies hasRole, hasAuthority, isAuthenticated, permitAll and access to the Authentication. Method security and web security use subclasses of it. For custom rules, prefer a bean referenced with @bean.method(...), and extend the root through a MethodSecurityExpressionHandler only when you need new methods inside the expression itself.