AccessDeniedException signals that a security check rejected an attempted protected operation. With method-level security enabled, a false authorization expression prevents the annotated method from being invoked. section 10.2
The exception appears in several method-security behaviors:
- @PreAuthorize throws it when its Spring Expression Language expression evaluates to false before invocation. section 10.2
- @PostAuthorize permits invocation but throws it when the expression rejects the returned result. section 10.2
- @PreFilter and @PostFilter constrain parameters or return values rather than preventing invocation outright. section 10.2
Protecting a tool
A getGameComplexity tool can require the PREMIUM_USER role with @PreAuthorize("hasRole('PREMIUM_USER')"). An authorized request returns the game complexity; an unauthorized request receives a response stating that the information is reserved for premium users.
