liveBook · Manning

Spring Expression Language

As explained in Spring AI in Action

Spring Expression Language is an expression language for querying and manipulating object graphs and evaluating conditions within Spring applications.

Spring Expression Language (SpEL) supplies boolean expressions for Spring authorization decisions. In a @PreAuthorize annotation, hasRole('PREMIUM_USER') permits a method call when the security context contains that role; otherwise Spring throws AccessDeniedException. section 10.2

@Tool(name = "getGameComplexity",
    description = "Returns a game's complexity/difficulty " +
        "given the game's title/name.")
@PreAuthorize("hasRole('PREMIUM_USER')")
public GameComplexityResponse getGameComplexity(
          @ToolParam(description="The title of the game")
          String gameTitle) {

  // ...

}

Secure a tool method by combining @PreAuthorize with a SpEL role expression. section 10.2

Use related method-security annotations for different authorization points:

  • @PostAuthorize allows invocation but checks the returned value afterward. section 10.2
  • @PreFilter and @PostFilter filter method parameters or return values with SpEL expressions. section 10.2

Restricting a tool

Enable method security with @EnableMethodSecurity, then annotate getGameComplexity—or a Function implementation's apply method—with @PreAuthorize("hasRole('PREMIUM_USER')"). Premium users receive the complexity; other users receive an access-denied response. section 10.2

How Spring AI in Action covers it

How deep it goes
Defines expression outcomes and shows role checks, then points to broader Spring Security expressions without detailing their syntax.
Examples
Uses getGameComplexity, the PREMIUM_USER role, @PreAuthorize, and authenticated HTTP requests as concrete examples.
What it assumes
Assumes an existing security configuration and teaches where to enable method security and place authorization expressions.
What you can do after
Operate method-level authorization by enabling security and attaching SpEL expressions to protected methods.
How it connects
Connects SpEL with Spring Security, method-level security, authorization annotations, and security contexts.

Also explained in

Compare how 2 books explain Spring Expression Language →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.