liveBook · Manning

Method-level security

As explained in Spring AI in Action

Method-level security is authorization enforced when application methods are invoked based on the caller's identity or authorities.

Method-level security authorizes bean-method invocations using the caller’s security context, preventing unauthorized users from reaching protected application capabilities or tools. section 10.2

Enable and apply it in Spring Security:

  • Add @EnableMethodSecurity to the security configuration. section 10.2
  • Annotate protected methods with @PreAuthorize and a SpEL expression. section 10.2
  • Use hasRole('PREMIUM_USER') to permit only callers holding the PREMIUM_USER role. section 10.2
  • Expect an AccessDeniedException when the expression evaluates to false. section 10.2

Protecting an LLM tool

Annotate a getGameComplexity tool or Function.apply() method with @PreAuthorize("hasRole('PREMIUM_USER')"). Premium users receive the game complexity; other users receive an authorization response instead. section 10.2

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {

  // ...

}

The configuration listing shows the single annotation needed to enable method-level security. section 10.2

How Spring AI in Action covers it

How deep it goes
Explains configuration, annotations, SpEL evaluation, role checks, and denied invocations.
Examples
Uses getGameComplexity, Function.apply(), PREMIUM_USER, and Burger Battle requests.
What it assumes
Assumes an existing Spring Security configuration and teaches method-level enablement from there.
What you can do after
Apply it via Spring Security annotations to restrict methods and tools by role.
How it connects
Connects Spring Security with Spring Expression Language, security contexts, roles, and tool invocation.

Also explained in

Compare how 2 books explain method-level security →

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.