Understanding CORS
In development, the frontend runs on
http://localhost:5173 (Vite) and the backend on http://localhost:8080 (Spring Boot). These are different origins, so CORS must be configured.Current CORS Configuration
The application’s CORS settings are configured inSecurityConfig.java:55-68:
Common CORS Errors
CORS policy: No 'Access-Control-Allow-Origin' header
CORS policy: No 'Access-Control-Allow-Origin' header
Error in Browser Console:Causes:
- Frontend origin not in allowed origins list
- CORS configuration not applied to all endpoints
- Backend not running or crashed
-
Verify frontend URL matches allowed origins:
- Check the URL in your browser address bar
- Must be exactly
http://localhost:5173orhttp://127.0.0.1:5173
- Add your origin to SecurityConfig.java:
- For development, allow all origins (NOT for production):
-
Verify CORS is applied globally:
- Check that
corsConfigurationSource()bean is defined - Ensure it’s registered in the security filter chain
- Check that
- Restart the backend after changes:
CORS policy: Request header field Authorization not allowed
CORS policy: Request header field Authorization not allowed
CORS policy: Method not allowed
CORS policy: Method not allowed
Error Message:Cause:
The HTTP method is not in the allowed methods list.Solution:
- Check allowed methods in SecurityConfig.java:58:
- Verify the method is included - all common methods should be there
- If needed, add missing method:
- Always include OPTIONS for preflight requests
CORS preflight request fails (OPTIONS)
CORS preflight request fails (OPTIONS)
Error Message:What is a preflight request?Browsers send an OPTIONS request before certain requests (those with custom headers or methods other than GET/POST) to check if the actual request is allowed.Solutions:
- Ensure OPTIONS method is allowed:
- Set maxAge to cache preflight responses:
- Already configured in line 63
- Check that CSRF is disabled (required for CORS):
- In
SecurityConfig.java:27:
- In
-
Verify OPTIONS requests aren’t blocked by security:
- OPTIONS requests should not require authentication
-
Check browser DevTools Network tab:
- Look for OPTIONS request
- Check response headers:
Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-Headers
Credentials mode issues
Credentials mode issues
Error Message:Cause:
Mismatch between frontend credentials setting and backend CORS configuration.Solutions:
- Backend: Ensure credentials are allowed:
- Check
SecurityConfig.java:62:
- Check
- Frontend: Send credentials if needed:
- If not using cookies, omit credentials:
Iquea Commerce uses JWT tokens stored in localStorage, so
credentials: 'include' is typically not needed unless you’re also using cookies.Development vs Production
CORS configuration for different environments
CORS configuration for different environments
Development Configuration:Allow localhost origins:Production Configuration:
- Use environment variables:
- In application.properties:
- Security best practices:
- Always use HTTPS in production
- Specify exact domains (no wildcards)
- Keep credentials enabled only if needed
- Set appropriate maxAge for caching
Nginx/Apache proxy CORS configuration
Nginx/Apache proxy CORS configuration
If using a reverse proxy in production:Nginx:Apache:
Debugging CORS Issues
Browser DevTools
- Open DevTools (F12) → Network tab
- Reproduce the error
- Look for the failed request
-
Check Request Headers:
Origin: http://localhost:5173Access-Control-Request-Method: POST(for preflight)Access-Control-Request-Headers: authorization(for preflight)
-
Check Response Headers:
Access-Control-Allow-Origin: http://localhost:5173Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, PATCHAccess-Control-Allow-Headers: Authorization, Content-TypeAccess-Control-Allow-Credentials: true
Testing with cURL
Access-Control-* headers in the response.
Common Mistakes
Quick Checklist
When troubleshooting CORS issues:- Backend is running and accessible
- Frontend origin is in
allowedOriginslist - All required HTTP methods are in
allowedMethods -
OPTIONSmethod is included for preflight - Required headers are in
allowedHeaders -
allowCredentialsmatches frontend usage - Backend was restarted after configuration changes
- Browser cache is cleared (Ctrl+Shift+Delete)
- No browser extensions interfering with requests
- CSRF is disabled in Spring Security
Additional Resources
- Common Issues - General troubleshooting
- Database Errors - Database issues
- MDN CORS Documentation
- Spring Security CORS