CorsConfiguration.setMaxAge在SpringBoot应用程序中不起作用

时间:2019-12-26 04:11:44

标签: spring-boot cors

我正在尝试使用 CorsConfiguration 在应用程序中设置缓存控制。理想情况下,当我在DHC或Rest Api中调用PostMan时,我应该获得Cache-Control Max Age 360​​0,但是我的响应低于以下要求: 缓存控制:无缓存,无存储,max-age = 0,必须重新验证

有人请帮助我。预先感谢。

以下是我的Application.java class

@EnableCaching
@SpringBootApplication
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

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

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setMaxAge(3600L);
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("POST");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
}

0 个答案:

没有答案