Resolving CORS errors in spring

CORS error
@CrossOrigin(origins = "*")
@GetMapping("/get-resource/{carId}")
public Car getCarDetails(@PathVariable long carId){
return carService.getCar(carId);
}
@CrossOrigin(origins = "localhost:4200")
@GetMapping("/get-resource/{carId}")
public Car getCarDetails(@PathVariable long carId){
return carService.getCar(carId);
}
@CrossOrigin(origins = {"http://localhost:4200", "http://someserver:8080"})
@CrossOrigin(origins = "localhost:4200")
@RestController
public class TransactionsController {
...
}
@CrossOrigin(origins = "*", methods = "*", allowedHeaders = "*", exposedHeaders = "*", allowCredentials = "*", maxAge = 30)
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedOrigins( Arrays.asList("http://localhost:4200"));
configuration.setAllowedMethods( Arrays.asList("GET", "PUT", "POST", "DELETE"));
configuration.setAllowCredentials(true);
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.configurationSource(corsConfigurationSource())
.and()
.csrf()
.disable()
...
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store