Disable HTTP OPTIONS method in spring boot application

后端 未结 3 498
悲&欢浪女
悲&欢浪女 2020-12-16 20:15

I had developed rest API on spring boot application. The APIs accept only GET , and POST , but on requesting using OPTIONS method , API responding 200 status (instead of 405

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 20:44

    Try this; in allowedMethods you can filter methods which are required:

    @Configuration
    public class CorsConfiguration {
    
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**")
                            .allowedOrigins(origins u want to allow)
                            .allowCredentials(false).allowedMethods("POST", "GET", "PUT");
    
                }
            };
        }
    }
    

提交回复
热议问题