liveBook · Manning

@EnableMethodSecurity

As explained in Spring AI in Action

@EnableMethodSecurity is a Spring Security annotation that enables authorization checks on secured method invocations.

@EnableMethodSecurity enables authorization checks on secured method invocations, helping prevent unauthorized users from invoking protected tools or accessing restricted data. section 10.2

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {

  // ...

}

Add @EnableMethodSecurity to the security configuration class alongside @Configuration and @EnableWebSecurity. section 10.2

After enabling it, place @PreAuthorize on methods that require authorization. Its SpEL expression must evaluate to true; otherwise Spring Security throws AccessDeniedException before invoking the method. section 10.2

Restricting a tool

Annotate a getGameComplexity tool or Function.apply() method with @PreAuthorize("hasRole('PREMIUM_USER')") to restrict invocation to users with the PREMIUM_USER role. section 10.2

How Spring AI in Action covers it

How deep it goes
Shows configuration and its relationship to @PreAuthorize, but defers broader annotation behavior and detailed security setup.
Examples
Uses a getGameComplexity tool and Function.apply() method restricted to the PREMIUM_USER role.
What it assumes
Assumes an existing Spring Security configuration and bean methods or tools that require protection.
What you can do after
Operate/configure method-level security by enabling it in SecurityConfig and securing selected methods with authorization expressions.
How it connects
Connects @PreAuthorize with Spring Expression Language, roles, Spring Security, and AccessDeniedException.

Also explained in

Compare how 2 books explain @EnableMethodSecurity →

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.