I am creating an Angular app, which makes use of a Spring Boot backend. I am using Spring Security to secure my REST API.
When I send my login credentials to my backend, my browser console logs the following error message:
login:1 Access to XMLHttpRequest at 'http://localhost:8080/v1/auth/login' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
From what I know, my backend, which is running on localhost:8080, does not allow a request coming from a different domain. I found this tutorial online, and have added the described bean to my security configuration:
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("http://localhost:4200"));
configuration.setAllowedMethods(List.of("GET", "PUT", "POST", "DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
I have, furthermore, added http.cors() to the configure(HttpSecurity http) method of the WebSecurityConfigurerAdapter, which my security configuration extends.
I was thinking this configuration would solve my problem, but nothing has changed. Can somebody help me out?