如何在Spring Boot中处理HTTP OPTIONS请求?

Jon*_*nik 27 spring spring-mvc http-options-method spring-boot

首先,我读过" 如何使用Spring MVC处理HTTP OPTIONS? "但答案似乎并不直接适用于Spring Boot.

看起来我应该这样做:

通过将其设置为dispatchOptionsRequest来配置dispatcherServlet true

但是如何做到这一点,因为我没有XML配置,或者DispatcherServlet我的代码中有任何类型的初始化器类(这个答案提到)?

@RestController类中,我有一个这样的方法,目前不会被调用.

@RequestMapping(value = "/foo", method = RequestMethod.OPTIONS)
public ResponseEntity options(HttpServletResponse response) {
    log.info("OPTIONS /foo called");
    response.setHeader("Allow", "HEAD,GET,PUT,OPTIONS");
    return new ResponseEntity(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 1.2.7.RELEASE; 一个简单的设置与Spring REST指南中的设置没有什么不同 .

Boh*_*rdt 26

选项1:Spring Boot属性(仅限Spring Boot 1.3.0+)

从Spring Boot 1.3.0开始,可以使用以下属性配置此行为:

spring.mvc.dispatch-options-request=true
Run Code Online (Sandbox Code Playgroud)

选项2:自定义 DispatcherServlet

DispatcherServlet在Spring Boot中定义DispatcherServletAutoConfiguration.您可以DispatcherServlet在配置类中的某个位置创建自己的bean,而不是自动配置中的bean:

@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setDispatchOptionsRequest(true);
    return dispatcherServlet;
}
Run Code Online (Sandbox Code Playgroud)

但请注意,定义DispatcherServletbean将禁用自动配置,因此您应手动定义autoconfiguration类中声明的其他bean,即ServletRegistrationBeanfor DispatcherServlet.

选项3: BeanPostProcessor

您可以创建在初始化bean之前BeanPostProcessordispatchOptionsRequest属性设置为的实现true.Yoy可以把它放在你的配置类中:

@Bean
public DispatcherServletBeanPostProcessor dispatcherServletBeanPostProcessor() {
    return new DispatcherServletBeanPostProcessor();
}

public static class DispatcherServletBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof DispatcherServlet) {
            ((DispatcherServlet) bean).setDispatchOptionsRequest(true);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
Run Code Online (Sandbox Code Playgroud)

选项4: SpringBootServletInitializer

如果你SpringBootServletInitializer在你的应用程序中有你可以做这样的事情来启用OPTIONS调度:

public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.getServletRegistration(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
                .setInitParameter("dispatchOptionsRequest", "true");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,只有在将应用程序作为WAR部署到Servlet容器中时才会起作用,因为在SpringBootServletInitializer使用main方法运行Spring Boot应用程序时不会执行代码.

  • 重要更新:从Spring Framework 4.3开始默认支持OPTIONS请求(默认情况下用于即将发布的Spring Boot 1.4版本),有关详细信息,请参阅https://jira.spring.io/browse/SPR-13130. (3认同)