尝试从React客户端访问spring-data-rest暴露的“ / profile”端点时,出现以下错误。我已经在存储库中启用了CORS,但是仍然可以获取错误,而我却能够访问“ http:// localhost:8083 / merchants”。提前致谢。
错误:
Access to XMLHttpRequest at 'http://localhost:8083/profile/merchants' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
最简单的方法是@CrossOrign("*")
在控制器类上方添加注释。
编辑 另一个方法是通过公开以下bean来全局启用CORS:
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// Don't do this in production, use a proper list of allowed origins
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
谢谢你的建议。但是问题是没有显式的控制器类,Spring-data-rest自动为存储库生成端点。在这种情况下,我不确定“ / profile”端点的控制器类在哪里
@SumanMondal我已经对我的答案进行了修改
谢谢,在全局启用CORS之后,现在问题已解决