【发布时间】:2017-10-08 06:43:33
【问题描述】:
我很难使用 Java 配置来获得全局 CORS 配置。我不确定这是否是一个已知问题,因为其他人也有同样的问题: Spring Global CORS configuration not working but Controller level config does
无论如何,让我进一步详细说明我正在尝试做的事情:
我正在使用 Spring 4.2.3.RELEASE(没有弹簧靴)。我正在尝试使用 Java config 配置全局 CORS 配置,以便我可以使用属性文件中的 @Value 注入 CORS 域(允许的来源)。因为据我所知,我无法在 mvc xml 命名空间中为 allowed-origin 注入值,如下图所示(如果您知道解决此问题的任何其他方法,请告诉我):
<mvc:cors>
<mvc:mapping path="/**" allowed-origins="${vrm.cors.domain}"
allowed-methods="*"
allowed-headers="*"
allow-credentials="false" max-age="3600"/>
</mvc:cors>
来自 Spring 参考文档here,我正在尝试使用 java config 配置 Global CORS 来解决问题。但是,我无法让 CORS 工作。代码在应用程序启动期间确实调用,但来自客户端的调用总是返回
请求中没有“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:3000” 访问
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Value("${vrm.cors.domain}")
//vrm.cors.domain=/**
private String corsDomain;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").
.allowedOrigins(vrm.cors.domain);
}
}
【问题讨论】:
-
为什么
vrm.cors.domain设置为/**?如果你想允许一切,不应该是*吗? -
对不起,这是我的示例代码中的错误。我已经更新了。
标签: java spring spring-mvc cors property-files