Skip to content

Enable/Disable CORS revisited [SPR-17598] #22130

Closed
@spring-projects-issues

Description

@spring-projects-issues

ayush-finix opened SPR-17598 and commented

Similar issue to #18266, however, disabling the cors processor doesn't work well enough.

We're building a proxy server where even the options requests should just be forwarded to the upstream, and the cors settings are somewhat interfering with this since there's CorsUtils.isPreflightRequest calls peppered around in various place (mostly referenced in the above ticket).

A few things I'd like to bring up:

After overriding RequestMappingHandlerMapping like

public class CorsNoopHandlerMapping extends RequestMappingHandlerMapping {

  public CorsNoopHandlerMapping() {
    setOrder(0);  // Make it override the default handler mapping.
  }

  @Override
  protected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request,
      HandlerExecutionChain chain, CorsConfiguration config) {
    return chain;  // Return the same chain it uses for everything else.
  }
}

in X extends WebMvcConfigurationSupport

@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
  return new CorsNoopHandlerMapping();
}

Disabling the cors processor and setting spring.mvc.dispatch-options-request=true still resolves a controller like the below to method 2

 

@RequestMapping(path = "/**", method = {RequestMethod.OPTIONS})
 method1() {}

@RequestMapping(path = "/**", method = {RequestMethod.DELETE, RequestMethod.GET, RequestMethod.HEAD, RequestMethod.POST, RequestMethod.PATCH, RequestMethod.PUT, RequestMethod.TRACE})
method2() {}

because the default RequestMethodsRequestCondition still checks preflight requests

https://github.com/spring-projects/spring-framework/blob/v5.1.2.RELEASE/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java#L108

Aside: Why are all the condition classes final?

 

There's seems to be a lot of hoops to jump through to just make any OPTIONS call (with Origin header for example) resolve to something like method1, and it's really unfortunate that there's no sane way to override the CorsUtils methods or just disable the feature entirely. Sorry if this isn't the right place to discuss this, and please let me know if there's any more information necessary.


Affects: 5.1.2, 5.1.3

Issue Links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions