r/SpringBoot • u/Future_Badger_2576 • Jul 18 '26
Question React + Spring Boot: If session auth is recommended, how is CSRF handled when frontend and backend are separate?
I am trying to understand how CSRF works in a setup where the backend is Spring Boot (API) and the frontend is React, both running as separate applications.
From what I have read, session-based authentication is recommended in many cases. The flow is:
User logs in.
Spring Boot creates a session.
Browser stores the session ID in an HttpOnly cookie.
But since cookies are sent automatically, doesn't that make the app vulnerable to CSRF?
My understanding of CSRF is: When the page is rendered, Backend generates a CSRF token and associates it with the user's session, and includes it in the HTML (hidden form field or meta tag). When the form is submitted, the backend compares the submitted token with the one stored in the session.
This makes sense when the backend renders the frontend (like Thymeleaf). But with a separate React app, how does React get the CSRF token and send it back? How is this usually implemented in Spring Boot?
Also, if storing a JWT in a cookie also makes it vulnerable to CSRF, where is a JWT supposed to be stored? I've seen people say localStorage, sessionStorage, and HttpOnly cookies, but each seems to have its own trade-offs.
I'm looking for the recommended approach for a Spring Boot + React setup.