Skip to content

CORS combine - override specific host defined in global conf with * in controller/method [SPR-15772] #20327

@spring-projects-issues

Description

@spring-projects-issues

Cyprian Gracz opened SPR-15772 and commented

In my Spring Boot I have global CORS config:

@Configuration
@EnableWebMvc
public class CorsConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		CorsRegistration config = registry.addMapping("/**");
		config.allowedOrigins("http://domain.com");
	}
}

Then I try to allow any origin to one of controllers:

@RestController
@CrossOrigin("*")
public class OpenController {
}

Because of this line:

	private List<String> combine(@Nullable List<String> source, @Nullable List<String> other) {
		if (other == null || other.contains(ALL)) {
			return (source != null ? source : Collections.emptyList());
}

in https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java#L373

CorsConfiguration gets "http://domain.com" as allowed domain because controller specifies ALL ( * ) as allowed origin, so it's "ignored".

Is this desired behaviour? Shouldn't controllers/methods always override global config?


Issue Links:

Referenced from: commits 0075f13

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions