Hello.
I’m trying the example showed in: Securing Angular + Spring Boot App with Okta but i have the following error when i try to access the API rest from the Angular App:
"Access to XMLHttpRequest at 'http://localhost:8080/contacts' 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."
Thanks in advance for any help.
You need a CORS filter in Spring Boot that allows requests from Angular. Try this tutorial: https://developer.okta.com/blog/2020/01/06/crud-angular-9-spring-boot-2
Hi.
Thanks, that solved the problem.
I change the next:
http.cors().configurationSource(request -> new CorsConfiguration(corsConfiguratione()));
http.antMatcher("/**")
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/").permitAll()
.anyRequest().authenticated();
http.oauth2ResourceServer();
By:
http.antMatcher("/**")
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/").permitAll()
.anyRequest().authenticated();
http.oauth2ResourceServer().jwt();
and i have added the next bean in the main class:
@Bean
public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(Collections.singletonList("http://localhost:4200"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
system
Closed
January 23, 2024, 11:30pm
4
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.