angular - Spring boot - 预检响应没有 HTTP ok 状态

标签 angular spring-boot spring-security cors preflight

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

正如我所读到的,这是因为我正在向这个请求添加自定义 header ,这需要完成,因为我使用的是 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请求被转换为 OPTIONS一个定制的 bec headersSpring Security .

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

谢谢。

最佳答案

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

我的 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();
}

关于angular - Spring boot - 预检响应没有 HTTP ok 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52181131/

相关文章:

javascript - Angular2 - 从窗口点击检测实体点击?

javascript - 在 Ionic 2/Angular 2 beta 10 中访问窗口对象

Spring Boot 应用程序失败,退出代码为 0

spring-boot - 我可以在同一台机器上运行多个Elasticsearch实例进行日志聚合吗?

java - 属性 'security.basic.enabled' 已弃用 : The security auto-configuration is no longer customizable

java.lang.ClassNotFoundException : org. springframework.core.convert.support.PropertyTypeDescriptor

angular - "@angular/core/testing"' has no exported member ' waitForAsync'

spring-boot - 如何在部署在 PCF 云中的 Spring Boot 应用程序中设置 Java 代理

java - Spring Boot 与 Spring Security 登录表单

javascript - Angular 4 : changing url, 但未呈现组件