【发布时间】:2021-04-11 09:31:51
【问题描述】:
我正在尝试通过本教程学习弹簧:
https://spring.io/guides/tutorials/react-and-spring-data-rest/
但我将前端应用程序作为一个单独的应用程序。 调用时:http://localhost:8080/api/employees
如何全局启用 CORS?谢谢!
在应用程序中尝试过:
@Bean
public WebMvcConfigurer configurer()
{
return new WebMvcConfigurer()
{
@Override
public void addCorsMappings(CorsRegistry registry)
{
registry.addMapping("/api/*").allowedOrigins("http://localhost:8000");
}
};
}
但没用
使用自定义 @RestController 进行了一些测试,如下所示:
https://spring.io/guides/gs/rest-service-cors/#global-cors-configuration 并从外部前端应用程序调用 http://localhost:8080/api/greeting 工作正常,只有那些通过 spring 自动生成的其余端点 CRUD 不允许 CORS 存在.. 如何避免这个问题?
【问题讨论】:
-
没有得到您的问题?
from external frontend app works fine, only those rest endpoints CRUD auto generated via spring are not allowed CORS there.. How to avoid this issue ?
标签: spring-boot