Spring boot - 预检响应没有 HTTP 正常状态

Wro*_*ong 5 spring-security cors preflight spring-boot angular

我正在使用 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢。

小智 4

我能够像这样解决这个问题:

我的 WebMvc 配置:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**");
    }

}
Run Code Online (Sandbox Code Playgroud)

我的安全配置:

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

  • 太棒了,你能给出答案!我希望你不介意一些指示吗?当你做出答案时,你应该尝试寻找来源并解释问题最初发生的原因、你如何修复它以及它为何有效。这会给你带来更多的代表!再次强调:很高兴您的加入,并且您正在努力提供帮助,而这也正是我正在努力做的! (2认同)
  • 这一行为我修复了它: `.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()` (2认同)