DEV Community

RajarshiG
RajarshiG

Posted on

From Chaos to Deployment: Fixing Cross-Origin Auth in React + Spring Boot (My 3-Day 401 Debug Story)

I recently finished building a full-stack Library Management System, with:

  • ๐Ÿ–ฅ๏ธ Frontend in React, deployed on Vercel
  • ๐Ÿ”ง Backend in Spring Boot, deployed on Railway

At the start, I was excited. But then... deployment hit me like a wall.


๐Ÿงฉ The Struggles

I didn't know:

  • How to deploy the backend and frontend separately
  • How to set up CORS and cookies properly
  • Why my app worked in Firefox, but failed with 401 errors in Chrome

I tried debugging:

  • Spring Security
  • Session management
  • Axios headers and cookies

I even rewrote parts of my code, thinking the problem was in my auth flowโ€ฆ

Turns out, Chrome had third-party cookies disabled. ๐Ÿ˜ค

Three. Days. Gone.


๐Ÿ” What I Learned

  1. Spring Security from Scratch

    I learned how to create a SecurityFilterChain and handle login with session-based auth using a custom success handler.

  2. Backend over LocalStorage

    Initially, I was storing the user in localStorage and using it for validation.

    I later replaced this with a backend call to /check-auth, making it more secure and reliable.

  3. CORS + Cookie Configuration

   .allowedOrigins("https://my-frontend.vercel.app")
   .allowCredentials(true)
Enter fullscreen mode Exit fullscreen mode

Axios:

withCredentials: true
Enter fullscreen mode Exit fullscreen mode
  1. Cookie Headers
  • SameSite=None
  • Secure
  • HttpOnly

All crucial for cross-origin session persistence.


โœ… Final Setup

  • ๐ŸŒ Frontend: React + Vercel
  • ๐Ÿ”ง Backend: Spring Boot + Railway
  • ๐Ÿ” Auth: Session-based, cookie-secured
  • ๐Ÿง  Learned: Spring Security, Session Auth, CORS, Deployment practices

๐ŸŽฏ Next Goal

Now that itโ€™s stable, Iโ€™m planning to migrate the backend to AWS EC2 for a more production-like deployment.

Wish me luck ๐Ÿคž


If youโ€™ve ever been stuck debugging for days over something silly โ€” youโ€™re not alone. Keep going. It clicks eventually โœจ

#SpringBoot #ReactJS #WebDev #FullStack #SpringSecurity #Debugging #AWS #DevJourney

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.