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
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
ExceptionTranslationFilter. Its pseudocode, from the architecture docs, is short enough to read as the rule: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). 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.
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):
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.
curl -i https://httpbin.org/status/403
# Expect: HTTP/2 403. Use this to confirm your terminal prints the status line.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:<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}"/>.csrf((csrf) -> csrf
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
);@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:
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.
3 questions people ask next, answered from the sources on this page.
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.
It is a shortcut for hasAuthority with the ROLE_ prefix added, so the user needs the ROLE_ADMIN authority.
The first one listed. The filter applies only the first match, so put specific patterns before general ones.
Run the free audit, then approve the fixes. Every change can be undone.