Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the role of the SecurityExpressionRoot class?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 21, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • What SecurityExpressionRoot provides
  • How the expression is evaluated
  • Enabling method security correctly
  • Adding custom logic: reference a bean from the expression
  • Adding custom logic: a custom expression root
  • Common mistakes with security expressions
  • Frequently Asked Questions
    • What is the difference between hasRole and hasAuthority?
    • Can I use SecurityExpressionRoot with @Secured?
    • Do I have to extend SecurityExpressionRoot to add custom rules?
    • Where does the authentication property in an expression come from?
  • Summary

What SecurityExpressionRoot provides

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:

ExpressionMeaning
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, denyAllAlways 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, principalThe Authentication object and its principal.

How the expression is evaluated

For a method annotated with @PreAuthorize, the flow is:

  1. A proxy intercepts the call.
  2. A MethodSecurityExpressionHandler builds an evaluation context. Its root object is a MethodSecurityExpressionRoot holding the current Authentication.
  3. Method parameters become variables, available as #name, so you can write #id or #order.owner.
  4. The SpEL expression is evaluated. A false result throws AccessDeniedException.
java

Here hasRole('ADMIN') is a method on the root object, #username is the method argument and authentication.name reads the root's authentication property.

Enabling method security correctly

In Spring Security 6 and later, use @EnableMethodSecurity. It replaces @EnableGlobalMethodSecurity, which is deprecated. @PreAuthorize and @PostAuthorize are on by default.

java

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).

Adding custom logic: reference a bean from the expression

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.

java
java

The custom class is plain Java, so you can unit test it without a Spring Security context.

Adding custom logic: a custom expression root

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.

java

TenantExpressionRoot implements MethodSecurityExpressionOperations, forwards every standard method to the delegate, and adds its own methods:

java

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.

Common mistakes with security expressions

  • Using hasRole('ROLE_ADMIN'). The prefix is added for you, so use hasRole('ADMIN'), or hasAuthority('ROLE_ADMIN').
  • Calling a protected method from inside the same class. The call skips the proxy, so no check happens.
  • Forgetting that expressions only run when method security is enabled.
  • Doing database queries in expressions without thinking about cost. They run on every call.

Frequently Asked Questions

What is the difference between hasRole and hasAuthority?

hasRole('ADMIN') checks for the authority ROLE_ADMIN, because it adds the prefix. hasAuthority('ADMIN') checks the exact string you give.

Can I use SecurityExpressionRoot with @Secured?

No. @Secured takes role names and does not evaluate SpEL. Use @PreAuthorize for expressions.

Do I have to extend SecurityExpressionRoot to add custom rules?

No. Calling a bean with @beanName.method(...) inside the expression is simpler and easier to test.

Where does the authentication property in an expression come from?

It is a property of the root object, so authentication and principal resolve against the current Authentication held by SecurityExpressionRoot.

Summary

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.

Share