Free auditStart free
GuideError · HTTP status codes

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.

3 min
read
4
sources
Oct 30, 2026
updated
Scaplet Editorial Team
author
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
  2. Reproduce and check it in 2 minutes
  3. Fix CSRF 403s
  4. Fix authority and matcher 403s
  5. 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 withChatGPTClaudePerplexityGoogleAI Mode
02Check 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, 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.
03Fix 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
<input type="hidden"
	name="${_csrf.parameterName}"
	value="${_csrf.token}"/>
Cookie repository readable by JavaScript
.csrf((csrf) -> csrf
    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
);
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:

04Your 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.

05Explained

What it means for SEO and 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 and 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. 01Spring Security: Cross Site Request Forgery (CSRF)docs.spring.io · checked 2026-11-01
  2. 02Spring Security: Authorize HttpServletRequestsdocs.spring.io · checked 2026-11-01
  3. 03Spring Security: Architecture (ExceptionTranslationFilter)docs.spring.io · checked 2026-11-01
  4. 04How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Questions

Asked a lot.

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

01Why 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.

02What does hasRole("ADMIN") check?

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

03Which rule wins if two matchers overlap?

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

Free · 2 minutes

Tools point. Agencies advise. This one ships.

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

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane