liveBook · Manning

AccessDeniedException

As explained in Spring AI in Action

AccessDeniedException is an exception indicating that a security check rejected access to a protected operation.

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.

How Spring AI in Action covers it

How deep it goes
Defines pre- and post-invocation behavior and related filters, but does not detail exception handling or implementation internals.
Examples
Uses a PREMIUM_USER-protected game-complexity tool and authorized or unauthorized HTTP requests.
What it assumes
Assumes method-level security, annotations, SpEL expressions, and security roles; the authorization condition itself is demonstrated.
What you can do after
Diagnose/debug rejected method calls by checking the authorization expression and required role.
How it connects
Connects method-level security with @PreAuthorize, Spring Expression Language, roles, and the security context.

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.