带有CORS起源站点的Spring无法正常工作

时间:2018-11-22 15:23:44

标签: spring spring-security

我正在使用Spring 4.3.10.RELEASE,并且具有在Spring security CORS Filter中找到的当前CORS配置

@Bean
CorsConfigurationSource corsConfigurationSource() {
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Collections.singletonList("https://google.com"));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
    configuration.setAllowCredentials(true);
    configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

如果我使用:

configuration.setAllowedOrigins(Collections.singletonList("*"));

我可以从任何站点向API发出请求(如预期的那样),但是如果我尝试插入一个或两个站点并从那些站点请求某些内容,则会出现“缺少Access-Control-Allow-Origins”警告:< / p>

  

Pedido de origem cruzada bloqueado:políticada mesma origemnão被许可人有权https://localhost:8241/foo/使用。 (Motivo:cabeçalhoCORS'Access-Control-Allow-Origin'em falta)。

     

TypeError:尝试获取资源时出现NetworkError。

在这种情况下是否缺少某些东西?

0 个答案:

没有答案