且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-01-31 00:16:47

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

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

Option 1: Spring Boot properties (Spring Boot 1.3.0+ only)

Starting with Spring Boot 1.3.0 this behavior can be configured using following property:

spring.mvc.dispatch-options-request=true

选项 2:自定义 DispatcherServlet

Spring Boot 中的

DispatcherServletDispatcherServletAutoConfiguration 定义.你可以在你的配置类的某处创建你自己的 DispatcherServlet bean,它将被用来代替自动配置中的 bean:

Option 2: Custom DispatcherServlet

DispatcherServlet in Spring Boot is defined by DispatcherServletAutoConfiguration. You can create your own DispatcherServlet bean somewhere in your configuration classes, which will be used instead of the one in auto configuration:

@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setDispatchOptionsRequest(true);
    return dispatcherServlet;
}

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

But be aware that defining your DispatcherServlet bean will disable the auto configuration, so you should manually define other beans declared in the autoconfiguration class, namely the ServletRegistrationBean for DispatcherServlet.

您可以创建 BeanPostProcessor 实现,它将在 bean 初始化之前将 dispatchOptionsRequest 属性设置为 true.你可以把它放在你的配置类中的某个地方:

You can create BeanPostProcessor implementation which will set the dispatchOptionsRequest attribute to true before the bean is initialized. Yoy can put this somewhere in your configuration classes:

@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;
    }
}

选项 4:SpringBootServletInitializer

如果您的应用程序中有 SpringBootServletInitializer,您可以执行以下操作以启用 OPTIONS 分派:

Option 4: SpringBootServletInitializer

If you had SpringBootServletInitializer in your application you could do something like this to enable OPTIONS dispatch:

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");
    }
}

然而,这仅在您将应用程序作为 WAR 部署到 Servlet 容器中时才有效,因为在使用 main 方法运行 Spring Boot 应用程序时不会执行 SpringBootServletInitializer 代码.

That would however only work if you deployed your app as a WAR into Servlet container, as the SpringBootServletInitializer code is not executed when running your Spring Boot app using main method.