【问题标题】:Extension and spring boot configuration for cors failcors 的扩展和弹簧引导配置失败
【发布时间】: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


【解决方案1】:

您还应该检查您的 Spring Security 配置。 CORS 应该通过WebSecurityConfigurerAdapter 启用:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()...
    }
}

更多详情Spring official documentation

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2012-04-14
    • 2013-10-10
    • 2021-10-25
    • 2011-09-11
    • 2019-09-26
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多