SpringBoot 2 Migration Issue with CORS support
See original GitHub issueUsing SpringBoot 1.5.9 with this controller and configuration, CORS requests are allowed (work fine):
@CrossOrigin
@RestController
public class SampleController {
@GetMapping(path = "mypath")
public String something() {
return "foobar";
}
}
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/**").permitAll();
}
}
However when I migrated to SpringBoot v2.0.0, with the same controller and configration I now get errors in the latest version of Chrome to the same previously working requests:
Failed to load https://gateway.mydomain.com/mypath: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https://spa.mydomain.com' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Per this possibly dated StackOverflow answer, I’ve tried using this updated configuration, but that still does not work:
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/**").permitAll();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
How do we get the same CORS functionality possible out of the box with SpringBoot 1.5.9 in SpringBoot 2?
Is there a better way to resolve this without specifying the origin domain?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Global CORS configuration breaks when migrating to Spring ...
I'm using spring boot 2.0.2. I have the same issue, but I use the following code to fix it. Does anybody have the...
Read more >CORS support in Spring Framework
Spring Framework 4.2 GA provides first class support for CORS out-of-the-box, giving you an easier and more powerful way to configure it than ......
Read more >Spring Boot - CORS Support - Tutorialspoint
Spring Boot - CORS Support, Cross-Origin Resource Sharing (CORS) is a security concept that allows restricting the resources implemented in web browsers.
Read more >CORS with Spring - Baeldung
2. Controller Method CORS Configuration ... Enabling CORS is straightforward — just add the annotation @CrossOrigin. We can implement this in ...
Read more >Spring Boot: How to Solve Cross-domain Problems
1. Spring Boot cross-domain problems.Create a filter to solve cross-domain · 2. Cross-domain joining Cors based on WebMvcConfigurerAdapter configuration
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is due to the change made in Spring Framework for SPR-16130.
allowCredentials
now defaults to false which affects this logic inCorsConfiguration
:You can restore the behaviour of Spring Framework 4.3 and Spring Boot 1.5 by changing your
@CrossOrigin
annotation:/cc @sdeleuze
@wilkinsona you saved my day thanks alot