Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing CORS Headers for Static Resources in WebFlux #26495

Closed
EusthEnoptEron opened this issue Feb 2, 2021 · 2 comments
Closed

Missing CORS Headers for Static Resources in WebFlux #26495

EusthEnoptEron opened this issue Feb 2, 2021 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@EusthEnoptEron
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 2, 2021
@rstoyanchev rstoyanchev self-assigned this Feb 2, 2021
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 2, 2021
@rstoyanchev rstoyanchev added this to the 5.3.4 milestone Feb 2, 2021
@rstoyanchev rstoyanchev changed the title CORS mappings not working for ResourceHandlers when using WebFlux Missing CORS Headers for Static Resources in WebFlux Feb 2, 2021
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Feb 2, 2021

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.

@rstoyanchev
Copy link
Contributor

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.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed type: bug A general bug status: backported An issue that has been backported to maintenance branches labels Feb 2, 2021
This was referenced Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants