The CORS (Cross-Origin Resource Sharing) error occurs because your Vue.js app (running on http://localhost:5173 or similar) is trying to make requests to your Spring Boot API (running on http://localhost:8080), and browsers block these cross-origin requests by default for security reasons.
Here are several ways to configure CORS in your Spring Boot application:
Method 1: Global CORS Configuration (Recommended)
Create a configuration class to enable CORS globally:
package com.yourpackage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// Allow credentials (cookies, authorization headers)
config.setAllowCredentials(true);
// Allow specific origins (replace with your Vue app's URL)
config.addAllowedOrigin("http://localhost:5173"); // Vite default
config.addAllowedOrigin("http://localhost:3000"); // Create React App default
config.addAllowedOrigin("http://localhost:8081"); // Alternative Vue port
// Allow all headers
config.addAllowedHeader("*");
// Allow all HTTP methods
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
Method 2: Using @CrossOrigin Annotation
Add the @CrossOrigin annotation directly to your controller or specific endpoints:
package com.yourpackage.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:3000"})
public class DescriptiveController {
@GetMapping("/descriptive")
public ResponseEntity<?> getDescriptiveAnalytics(
@RequestParam String datasetPath,
@RequestParam String column) {
// Your existing logic here
return ResponseEntity.ok(result);
}
}
Or on individual methods:
@GetMapping("/descriptive")
@CrossOrigin(origins = "http://localhost:5173")
public ResponseEntity<?> getDescriptiveAnalytics(
@RequestParam String datasetPath,
@RequestParam String column) {
// Your existing logic here
return ResponseEntity.ok(result);
}
Method 3: Using WebMvcConfigurer (Alternative Global Config)
package com.yourpackage.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:5173", "http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
}
Method 4: Application Properties Configuration
Add these properties to your application.properties or application.yml:
application.properties:
# CORS Configuration
spring.web.cors.allowed-origins=http://localhost:5173,http://localhost:3000
spring.web.cors.allowed-methods=GET,POST,PUT,DELETE,OPTIONS
spring.web.cors.allowed-headers=*
spring.web.cors.allow-credentials=true
application.yml:
spring:
web:
cors:
allowed-origins:
- http://localhost:5173
- http://localhost:3000
allowed-methods:
- GET
- POST
- PUT
- DELETE
- OPTIONS
allowed-headers: "*"
allow-credentials: true
Method 5: Quick Development Fix (Not for Production)
For development only, you can allow all origins:
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOriginPattern("*"); // Allow all origins
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
Recommended Approach
I recommend Method 1 (Global CORS Configuration) because:
- Centralized: All CORS configuration is in one place
- Flexible: Easy to modify for different environments
- Secure: You explicitly specify allowed origins
- Maintainable: Clear separation of concerns
Important Notes
-
Replace the origins with your actual Vue.js development server URL (check your terminal when running npm run dev)
-
For production, replace http://localhost:5173 with your actual domain:
config.addAllowedOrigin("https://yourdomain.com");
-
Security: Never use * for allowedOrigins in production when allowCredentials is true
-
Restart your Spring Boot application after adding the CORS configuration
Testing the Fix
After implementing one of these solutions:
- Restart your Spring Boot application
- Refresh your Vue.js application
- Try making the API request again
- Check the browser's Network tab to confirm the CORS headers are present
The response should now include headers like:
Access-Control-Allow-Origin: http://localhost:5173
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: *
This should resolve the CORS error and allow your Vue.js application to successfully communicate with your Spring Boot API.