# AccessDeniedException: Access is denied (Spring Security 403)

**Code:** `Access is denied` · **Category:** Authentication · **Seen on:** Java, Spring Boot

The user is authenticated but lacks the role or authority required for the endpoint, so Spring returns 403.

## Meaning

This is authorisation, not login. The `SecurityFilterChain` or `@PreAuthorize` rule needs a role the principal does not have. A common trap is the `ROLE_` prefix: `hasRole('ADMIN')` checks `ROLE_ADMIN`.

## Common causes

- Principal missing the required authority
- `ROLE_` prefix mismatch (`hasRole` vs `hasAuthority`)
- Rule order in `authorizeHttpRequests`
- Method security on but roles not loaded

## Quick fix

1. Print `SecurityContextHolder.getContext().getAuthentication().getAuthorities()`
2. Use `hasAuthority('ROLE_ADMIN')` or add the prefix
3. Put specific matchers before `anyRequest()`

## Detailed fix by platform

### Spring Boot

1. `.requestMatchers("/admin/**").hasRole("ADMIN")` and grant `ROLE_ADMIN` in `UserDetails`

## How to diagnose

1. **Authorities** — What authorities does the principal hold?
2. **Prefix** — ROLE_ prefix correct?
3. **Order** — Matcher order correct?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/auth/spring-access-denied)
