【发布时间】:2020-07-04 09:00:11
【问题描述】:
我使用 angular 9 的 spring boot 2.2。
我在 firefox 和 chrome 中使用 cors 扩展
我也穿上了弹簧靴
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
我仍然有这个错误
在“http://localhost:8081/credit/1/cancel”访问 XMLHttpRequest 来自原点“http://localhost:4200”已被 CORS 策略阻止: 对预检请求的响应未通过访问控制检查:它 没有 HTTP ok 状态。
编辑
试过
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().build();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
最糟糕的
【问题讨论】:
-
"它没有 HTTP ok 状态。" 8081 端口上的 HTTP 服务器是否已启动并正在处理请求?
-
8081 是我的 spring boot 应用程序,4200 是 nodejs
-
如果你直接请求页面(不是跨域),我会接受它,它会响应 200 OK? CORS 标头设置为什么?也不确定您使用什么 CORS 插件...但 Chrome 不支持本地主机的 CORS
标签: angular spring-boot cors