# CORS problem with Okta, Angular and Spring

**URL:** <https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243>\
**Category:** Questions\
**Tags:** spring, angular\
**Created:** [May 5, 2020, 6:05am UTC](https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243 "2020-05-05T06:05:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dukeim](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@dukeim](https://devforum.okta.com/u/dukeim)\
**Post date:** [May 5, 2020, 6:05am UTC](https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243/1 "2020-05-05T06:05:28Z")

</div>

Hello.

I’m trying the example showed in: [Securing Angular + Spring Boot App with Okta](https://medium.com/@raghavendra.pes/securing-angular-spring-boot-application-with-okta-671e983e5b6) 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."`

 ![CORS problem](https://global.discourse-cdn.com/oktadev/original/2X/b/b82b7e86022e7c7542633fc657a86a277005f851.png)

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/mraible/32/37_2.png) [@mraible](https://devforum.okta.com/u/mraible)\
**Post date:** [May 5, 2020, 3:16pm UTC](https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243/2 "2020-05-05T15:16:34Z")

</div>

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](https://developer.okta.com/blog/2020/01/06/crud-angular-9-spring-boot-2)

---

<div class="post-metadata">

**Author:** ![dukeim](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@dukeim](https://devforum.okta.com/u/dukeim)\
**Post date:** [May 5, 2020, 6:45pm UTC](https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243/3 "2020-05-05T18:45:54Z")

</div>

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;
	}
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [January 23, 2024, 11:30pm UTC](https://devforum.okta.com/t/cors-problem-with-okta-angular-and-spring/9243/4 "2024-01-23T23:30:31Z")

</div>

Closed on January 23, 2024, 11:30pm UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
