首页 > 解决方案 > 已被 CORS 策略阻止:对预检请求的响应未通过

问题描述

我已经使用 angular 和 springboot 创建了一个应用程序,用于使用 spring security 进行基本身份验证,但我收到 401 错误 ..我是 springboot 的新手

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter{ 

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

“从源 ' http://localhost:4200 '访问 XMLHttpRequest 在 ' http://localhost:8080/hello-world/path-variable/MSD '已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。”

标签: javaspringspring-bootauthenticationangular7

解决方案


I also had the same issue with angulat7 and spring boot I resolved by adding the following configuration

 @Configuration
    public class SpringDataRestConfiguration extends RepositoryRestConfigurerAdapter {

        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

        config.getCorsRegistry().addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("GET", "POST", "PATCH", "PUT", "DELETE");
        }

    }

推荐阅读