Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Spring引导-飞行前的响应没有HTTP ok状态_Angular_Spring Boot_Spring Security_Cors_Preflight - Fatal编程技术网

Angular Spring引导-飞行前的响应没有HTTP ok状态

Angular Spring引导-飞行前的响应没有HTTP ok状态,angular,spring-boot,spring-security,cors,preflight,Angular,Spring Boot,Spring Security,Cors,Preflight,我正在用Angular 5创建一个web,每次尝试执行GET请求时都会出现此错误。我在这里读了很多答案,但没有一个对我有用 正如我所读到的,这是因为我正在向这个请求添加自定义头,这需要完成,因为我使用的是Spring安全性,我认为这是导致问题的原因。这是我目前的Spring安全配置,我通过阅读问题制作而成,但仍然不起作用,我不知道我是否在其中做错了什么: import org.springframework.context.annotation.Bean; 导入org.springframewo

我正在用Angular 5创建一个web,每次尝试执行
GET
请求时都会出现此错误。我在这里读了很多答案,但没有一个对我有用

正如我所读到的,这是因为我正在向这个请求添加自定义头,这需要完成,因为我使用的是Spring安全性,我认为这是导致问题的原因。这是我目前的Spring安全配置,我通过阅读问题制作而成,但仍然不起作用,我不知道我是否在其中做错了什么:

import org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.http.HttpMethod;
导入org.springframework.security.config.annotation.web.builders.HttpSecurity;
导入org.springframework.security.config.annotation.web.builders.WebSecurity;
导入org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
导入org.springframework.security.config.annotation.web.configuration.websecurityConfigureAdapter;
导入org.springframework.security.config.http.SessionCreationPolicy;
导入org.springframework.web.cors.cors配置;
导入org.springframework.web.cors.cors配置源;
导入org.springframework.web.cors.UrlBasedCorsConfigurationSource;
导入java.util.array;
@配置
@启用Web安全性
公共类安全配置扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.cors()
.及()
.authorizeRequests().anyRequest().permitAll()
.及()
.sessionManagement().sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.csrf().disable();
}
@凌驾
public void configure(WebSecurity web)引发异常
{
忽略().antMatchers(HttpMethod.OPTIONS,“/**”);
}
@豆子
CorsConfiguration源CorsConfiguration源(){
CorsConfiguration配置=新的CorsConfiguration();
configuration.setAllowedOriginates(Arrays.asList(“*”));
setAllowedMethods(Arrays.asList(“GET”、“POST”、“PUT”、“PATCH”、“DELETE”、“OPTIONS”));
setAllowedHeader(Arrays.asList(“授权”、“内容类型”、“x-auth-token”));
setExposedHeaders(Arrays.asList(“x-auth-token”);
UrlBasedCorsConfigurationSource=新的UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(“/**”,配置);
返回源;
}
}
希望你能帮我解决这个问题,我已经为此挣扎了好几天了。我认为这里的问题显然是
CORS
,我的
GET
请求被转换成一个
选项
一个自定义
标题
Spring Security

另外,我想提到的是,我正在使用春季球衣靴


谢谢。

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

我的WebMVC配置:

@配置
@EnableWebMvc
公共类WebConfiguration扩展了WebMVCCConfiguration支持{
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表){
registry.addResourceHandler(“/**”);
}
}
我的安全配置:

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http.cors().disable()
.授权请求()
.antMatchers(HttpMethod.OPTIONS,“/**”).permitAll()
.anyRequest()
.fullyaauthenticated()
.及()
.httpBasic()
.及()
.csrf().disable();
}

添加以下配置

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

太棒了,你在回答!我希望你不会介意我给你提几点建议吧?当你回答问题时,你应该设法找到问题的根源,并解释为什么问题首先会发生,你是如何解决的,以及它为什么会起作用。这会给你带来更多的声誉!再次重申:有你在船上很高兴,你正试图帮助我,这也是我试图做的!第一个类@fabiano做什么?这行为我修复了它:
.antMatchers(HttpMethod.OPTIONS,“/**”).permitAll()