@PreAuthorize restricts a method invocation until a Spring Expression Language (SpEL) expression evaluates to true. Enable method-level security with @EnableMethodSecurity, then place the annotation on secured bean methods. section 10.2
Authorization behavior follows the expression result:
- A true expression permits the invocation. section 10.2
- A false expression throws AccessDeniedException before the method runs. section 10.2
Restricting an AI tool
Annotate getGameComplexity with @PreAuthorize("hasRole('PREMIUM_USER')") to limit the tool to users holding the PREMIUM_USER role. The same annotation can secure apply() when the tool is implemented as a Function. 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) {
// ...
}
Shows method-security enablement and a role-protected @Tool declaration. section 10.2

