Skip to content

Make OPTIONS/TRACE request handling configurable #4300

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ protected static class DispatcherServletConfiguration {
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setDispatchOptionsRequest(
this.webMvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(
this.webMvcProperties.isDispatchTraceRequest());
dispatcherServlet.setThrowExceptionIfNoHandlerFound(
this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
return dispatcherServlet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public class WebMvcProperties {
*/
private String dateFormat;

/**
* If TRACE requests should go through the regular dispatching chain.
*/
private boolean dispatchTraceRequest = false;

/**
* If OPTIONS requests should go through the regular dispatching chain.
*/
private boolean dispatchOptionsRequest = false;

/**
* If the content of the "default" model should be ignored during redirect scenarios.
*/
Expand Down Expand Up @@ -105,6 +115,22 @@ public void setThrowExceptionIfNoHandlerFound(
this.throwExceptionIfNoHandlerFound = throwExceptionIfNoHandlerFound;
}

public boolean isDispatchOptionsRequest() {
return this.dispatchOptionsRequest;
}

public void setDispatchOptionsRequest(boolean dispatchOptionsRequest) {
this.dispatchOptionsRequest = dispatchOptionsRequest;
}

public boolean isDispatchTraceRequest() {
return this.dispatchTraceRequest;
}

public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
this.dispatchTraceRequest = dispatchTraceRequest;
}

public Async getAsync() {
return this.async;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,18 @@ public void dispatcherServletConfig() {
DispatcherServletAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.throw-exception-if-no-handler-found:true");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.dispatch-options-request:true");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.dispatch-trace-request:true");
this.context.refresh();
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
assertEquals(true, new DirectFieldAccessor(bean)
.getPropertyValue("throwExceptionIfNoHandlerFound"));
assertEquals(true, new DirectFieldAccessor(bean)
.getPropertyValue("dispatchOptionsRequest"));
assertEquals(true, new DirectFieldAccessor(bean)
.getPropertyValue("dispatchTraceRequest"));
}

@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ content into your application; rather pick only the properties that you need.
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.mvc.ignore-default-model-on-redirect=true # if the content of the "default" model should be ignored redirects
spring.mvc.throw-exception-if-no-handler-found=false # if a "NoHandlerFoundException" should be thrown if no Handler was found to process a request
spring.mvc.dispatch-trace-request=false # if TRACE requests should go through the regular dispatching chain
spring.mvc.dispatch-options-request=false # if OPTIONS requests should go through the regular dispatching chain
spring.mvc.async.request-timeout= # async request timeout in milliseconds
spring.mvc.view.prefix= # MVC view prefix
spring.mvc.view.suffix= # ... and suffix
Expand Down