java - 已被 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 状态。”
解决方案
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");
}
}
推荐阅读
- flutter - 在模型类中重用 setter
- javascript - 类不理解使用 typescript 的 Mongoose 模型
- c# - 如何从剃刀的下拉列表中获取依赖值
- python - 视觉代码环境错误当我打开它时
- html - 如何从 URL 将 XML 嵌入到我的 HTML 网页中?
- c# - *已解决*将包含更新模型对象的列表从视图返回到控制器
- database - 在将 JSONB[] 数据类型移入和移出 Postgres 时,在 SSIS 数据流中使用的正确数据类型是什么?
- angular - 以编程方式触发 Angular 材质图标按钮上的波纹
- snakemake - Snakemake - Tibanna 配置支持
- embedded - CC2538 SoC如何进行OTA编程?