【问题标题】:Spring boot - Response for preflight does not have HTTP ok statusSpring boot - 预检响应没有 HTTP ok 状态
【发布时间】:2019-02-10 08:42:56
【问题描述】:

我正在使用 Angular 5 制作网络,每次尝试执行 GET 请求时都会收到此错误。我在这里阅读了大量的答案,但没有一个对我有用。

正如我所读的那样,因为我正在向这个请求添加自定义标头,这需要完成,因为我使用的是 Spring Security,我认为这是导致问题的原因。这是我当前的 Spring Security 配置,我是通过阅读问题做出的,但仍然无法正常工作,我不知道我是否做错了什么:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                .authorizeRequests().anyRequest().permitAll()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable();
    }

    @Override
    public void configure(WebSecurity web ) throws Exception
    {
        web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
        configuration.setExposedHeaders(Arrays.asList("x-auth-token"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

希望你能帮忙解决这个问题,因为我已经为此苦苦挣扎了几天。我认为显然这里的问题是CORS,我的GET 请求被转换为自定义headersSpring Security 的一个OPTIONS

另外我想提一下,我正在使用带有 Jersey 的 Spring Boot。

谢谢。

【问题讨论】:

    标签: angular spring-boot spring-security cors preflight


    【解决方案1】:

    我可以这样解决这个问题:

    我的 WebMvc 配置:

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

    我的安全配置:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
          http.cors().disable()
              .authorizeRequests()
              .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
              .anyRequest()
              .fullyAuthenticated()
              .and()
              .httpBasic()
              .and()
              .csrf().disable();
    }
    

    【讨论】:

    • 您的回答真棒!我希望你不会介意只是一些指示?当您做出回答时,您应该尝试寻找来源并首先解释问题发生的原因,您如何解决它以及它为什么会起作用。这会让你获得更多的代表!再说一遍:很高兴有你加入,你正在努力提供帮助,而这正是我想要做的!
    • 头等舱是做什么的@fabiano?
    • 这条线为我修好了:.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
    【解决方案2】:

    添加如下配置,

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

    【讨论】:

      猜你喜欢
      • 2019-04-03
      • 2019-02-04
      • 1970-01-01
      • 2019-02-02
      • 2019-03-15
      • 1970-01-01
      • 2019-02-27
      • 2021-09-06
      • 2020-08-27
      相关资源
      最近更新 更多