Spring boot - Response for preflight does not have HTTP ok status

前端 未结 2 534
悲哀的现实
悲哀的现实 2021-01-22 03:50

I am making a web with Angular 5 and I am getting this error every time I try to do a GET request. I\'ve read tons of tons of answers here and none of them working

相关标签:
2条回答
  • 2021-01-22 04:31

    Add following configuration ,

    @Configuration
    public class CorsConfig {
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
                            .allowedHeaders("*");
                }
            };
        }
    }
    
    0 讨论(0)
  • 2021-01-22 04:35

    I was able to solve this problem like this:

    My WebMvcConfiguration:

    @Configuration
    @EnableWebMvc
    public class WebConfiguration extends WebMvcConfigurationSupport {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**");
        }
    
    }
    

    My Security Configuration:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
          http.cors().disable()
              .authorizeRequests()
              .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
              .anyRequest()
              .fullyAuthenticated()
              .and()
              .httpBasic()
              .and()
              .csrf().disable();
    }
    
    0 讨论(0)
提交回复
热议问题