Affects: 5.3.3 Affected component: WebFlux
I created a configuration that roughly looks like this:
@EnableWebFlux
@Configuration
class WebfluxConfig : WebFluxConfigurer {
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/public/**")
.addResourceLocations("classpath:/public/")
}
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/public/**")
.allowedMethods("GET", "OPTIONS")
.allowedOriginPatterns("*")
}
}
Expectation:
A GET / OPTIONS request to http://localhost:8080/public/path/to/file
should return with CORS headers.
Actual result: CORS headers seem to be missing.
Possible cause:
In the bean declaration there seems to be no call to setCorsConfigurations()
/ configureAbstractHandlerMapping()
, although there is in case of RequestMappingHandler
and RouterFunctionMapping
.
FWIW, in the MVC counterpart, this call seems to exist.
I tried adding that line using a breakpoint, after which CORS started working for me.
Sorry if I'm missing something obvious -- I'm just getting started with WebFlux. 😄 For the time being I guess I'll resort to a WebFilter.
Comment From: rstoyanchev
ResourceWebHandler
needs to be declared as CorsConfigurationSource
in order to be considered as a handler that supports CORS. It looks like that's missing right now unlike the ResourceHttpRequestHandler
for Spring MVC.
Comment From: rstoyanchev
Never mind that. It's unrelated to ResourceWebHandler
implenting CorsConfigurationSource
. It seems that indeed WebFluxConfigurer#addCorsMapping
simply doesn't apply to static resources. The Javadoc mentions only annotated controllers and functional endpoints.