# 403 Forbidden in Spring Boot

> A Spring Boot 403 with Spring Security usually means a missing CSRF token or a failed authority rule. Reproduce it with curl and fix hasRole, matcher order and CSRF.

Canonical: https://scaplet.com/errors/http/403/spring-boot

Guide Error · HTTP status codes

A Spring Boot 403 with Spring Security usually means a missing CSRF token or a failed authority rule. Reproduce it with curl and fix hasRole, matcher order and CSRF.

3 min

read

4

sources

Oct 30, 2026

updated

Scaplet Editorial Team

author

403 4xx · Client error

The short answer

In a Spring Boot app with Spring Security, a 403 means the request was authenticated or not required to be, and then denied. The two common causes are a POST, PUT or DELETE without a valid CSRF token, and an authorization rule such as hasRole that the user does not meet. Check which one by testing with and without the token.

1. [Why Spring Security answers 403 instead of 401](#why-spring-security-answers-403-instead-of-401)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [Fix CSRF 403s](#fix-csrf-403s)
4. [Fix authority and matcher 403s](#fix-authority-and-matcher-403s)
5. [What it means for SEO and crawlers](#what-it-means-for-seo-and-crawlers)

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fspring-boot%20(%22403%20Forbidden%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fspring-boot%20(%22403%20Forbidden%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fspring-boot%20(%22403%20Forbidden%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F403%2Fspring-boot%20(%22403%20Forbidden%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Common causes of a Spring Security 403

## Why Spring Security answers 403 instead of 401

Spring Security splits denial into two cases inside `ExceptionTranslationFilter`. Its pseudocode, from the architecture docs, is short enough to read as the rule:

Symptom

### GET works, POST fails

Cause

CsrfFilter rejected a request with a missing or invalid token

Fix

Send the token, or disable CSRF only for a stateless API

Symptom

### Logged in, still 403 on an admin URL

Cause

The user lacks the required authority; hasRole("ADMIN") needs ROLE_ADMIN

Fix

Grant ROLE_ADMIN, or use hasAuthority("ADMIN") if your authority has no prefix

Symptom

### A path you permitted is denied

Cause

An earlier rule matched first; only the first match applies

Fix

Put specific matchers before general ones

Symptom

### Everything unlisted is 403

Cause

anyRequest().denyAll() as the catch-all

Fix

Add a rule for the path

ExceptionTranslationFilter (from the Spring Security docs)

```
try { filterChain.doFilter(request, response); } catch (AccessDeniedException | AuthenticationException ex) { if (!authenticated || ex instanceof AuthenticationException) { startAuthentication(); } else { accessDenied(); } }
```

Not authenticated means the entry point runs and you get a login redirect or a 401 (see [401 in Spring Boot](/errors/http/401/spring-boot)). Authenticated but refused means the `AccessDeniedHandler` runs, which is the 403. The authorization docs' test examples show the same split: an unauthenticated request is unauthorized, and an authenticated user lacking the authority is forbidden.

_02_Check it

## Reproduce and check it in 2 minutes

Against your own app, the fastest test separates CSRF from authority. Run the same authenticated call with a GET and a POST. This needs your running app (replace the port, path and credentials, and remove -u if you use form login):

Your own app: GET versus POST

```
curl -i -u user:password http://localhost:8080/api/orders curl -i -u user:password -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:8080/api/orders # Expect: GET returns 200 and POST returns 403 when CSRF is the cause. # If both return 403, the problem is an authority rule instead.
```

Turn on debug logging for `org.springframework.security` and rerun. The docs' debug excerpt shows `AccessDeniedHandlerImpl` logging that it is responding with a 403 [status code](/glossary/http-status-codes), which confirms the denial came from the access-denied path.

Public endpoint: a known 403

```
curl -i https://httpbin.org/status/403 # Expect: HTTP/2 403. Use this to confirm your terminal prints the status line.
```

_03_Fix it on your stack

## Fix CSRF 403s

The CSRF docs say that when the token is invalid or missing, an `AccessDeniedException` goes to the `AccessDeniedHandler`, and their tests assert 403 for both cases. For server-rendered forms, Thymeleaf and the Spring form tags add the token. Otherwise the docs show the hidden field:

Manual hidden field
Manual hidden field

```
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
```

Cookie repository readable by JavaScript
Cookie repository readable by JavaScript

```
.csrf((csrf) -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) );
```

Stateless API: disable, or exempt a path
Stateless API: disable, or exempt a path

```
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http // ... .csrf((csrf) -> csrf.disable()); return http.build(); } // or exempt only some endpoints: // .csrf((csrf) -> csrf.ignoringRequestMatchers("/api/*"))
```

For JavaScript clients, store the token in a cookie they can read. The cookie is `XSRF-TOKEN` and the default header is `X-XSRF-TOKEN`:

If the backend serves no browser traffic, the docs say it may choose to disable CSRF. You can also exempt matching endpoints only:

**Disable CSRF only where cookies are not the credential**The docs note that a backend serving browser traffic still requires CSRF protection. Disabling it on an app that relies on session cookies removes the check that stops forged form posts.

_04_Your call

## Fix authority and matcher 403s

The authorize-requests docs give this rule list. Two details cause most surprises: `hasRole("ADMIN")` requires the `ROLE_ADMIN` authority (you pass `"ADMIN"`, not `"ROLE_ADMIN"`), and `hasAllAuthorities` does not add the prefix.

If this fits

### It is an authority problem if

- GET and POST both return 403 for the same user
- The user has a role stored without the ROLE_ prefix
- A catch-all denyAll sits after your rules

Or

### It is CSRF if

- Only unsafe methods fail
- The same call works with the token or after disabling CSRF in a test profile
- The client is a browser or script that never fetched a token

Request rules from the Spring Security docs

```
http .authorizeHttpRequests((authorize) -> authorize .requestMatchers("/static/**", "/signup", "/about").permitAll() .requestMatchers("/admin/**").hasRole("ADMIN") .requestMatchers("/endpoint").hasAuthority("USER") .requestMatchers("/db/**").hasAllAuthorities("db", "ROLE_ADMIN") .anyRequest().denyAll() );
```

The filter processes these pairs in the order listed and applies only the first match, so keep `anyRequest()` last. `denyAll()` as the final rule is a deliberate allow-list: any path you forgot returns 403.

_05_Explained

## What it means for SEO and crawlers

[Googlebot](/ai-crawlers/googlebot) fetches pages without credentials, so the usual risk is an `authorizeHttpRequests` rule: a public page matched by `denyAll()` or a role rule returns a 4xx to crawlers, and Google ignores content from 4xx URLs. Google also says 401 and 403 do not slow crawling, so do not use them for that. Review your rule list for marketing paths, and see [403 Forbidden](/errors/http/403) and [4xx errors](/seo-issues/4xx-errors).

Sources · 4

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- spring.io
- google.com

1. 01 [Spring Security: Cross Site Request Forgery (CSRF)](https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html) docs.spring.io · checked 2026-11-01
2. 02 [Spring Security: Authorize HttpServletRequests](https://docs.spring.io/spring-security/reference/servlet/authorization/authorize-http-requests.html) docs.spring.io · checked 2026-11-01
3. 03 [Spring Security: Architecture (ExceptionTranslationFilter)](https://docs.spring.io/spring-security/reference/servlet/architecture.html) docs.spring.io · checked 2026-11-01
4. 04 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

Questions

## Asked a lot.

3 questions people ask next, answered from the sources on this page.

_01_ Why does my Spring Boot POST return 403 but GET works?

CsrfFilter requires a valid token on unsafe methods. A missing or invalid token results in an AccessDeniedException and a 403. Send the token or, for a stateless API, disable or narrow CSRF.

_02_ What does hasRole("ADMIN") check?

It is a shortcut for hasAuthority with the ROLE_ prefix added, so the user needs the ROLE_ADMIN authority.

_03_ Which rule wins if two matchers overlap?

The first one listed. The filter applies only the first match, so put specific patterns before general ones.

## Keep learning

[All errors](/errors)

[Learn 401 Unauthorized in Spring Boot HTTP status codes](/errors/http/401/spring-boot) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next 403 Forbidden HTTP status codes](/errors/http/403) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 403 errors
[403 Forbidden in IIS](/errors/http/403/iis) [403 Forbidden in Laravel](/errors/http/403/laravel) [403 Forbidden in nginx](/errors/http/403/nginx) [403 Forbidden in WordPress](/errors/http/403/wordpress) [403 Forbidden on Cloudflare](/errors/http/403/cloudflare)

Nearby in errors
[404 Not Found](/errors/http/404)

More in 403 Forbidden
[403 Forbidden in Apache](/errors/http/403/apache) [403 Forbidden in Chrome](/errors/http/403/chrome) [403 Forbidden in Django](/errors/http/403/django) [403 Forbidden in IIS](/errors/http/403/iis) [403 Forbidden in Laravel](/errors/http/403/laravel) [403 Forbidden in nginx](/errors/http/403/nginx) [403 Forbidden in Spring Boot](/errors/http/403/spring-boot) [403 Forbidden in WordPress](/errors/http/403/wordpress) [403 Forbidden on Cloudflare](/errors/http/403/cloudflare) [403 Forbidden on CloudFront](/errors/http/403/cloudfront)

Terms on this page
[HTTP status codes](/glossary/http-status-codes)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
