我在春季启动时遇到了CORS问题。我已经配置好了这样的CORSSpring Boot和CORS
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
我想可以启用所有头文件和其他东西。
它可以很好地与GET请求
$.get("someUrl, function(data, status){
console.log(data[0].latitude);
});
但每当我做出这样的
$.ajax({
url: 'someUrl',
type: 'post',
dataType: 'json',
crossDomain: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
},
data: object
});
我得到POST请求以下
OPTIONS XHR "someUrl" [HTTP/1.1 403 Forbidden 4ms]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at "someUrl".
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
我怎样才能解决这个问题呢?
不管是什么引起的问题,我不认为这是您曾经参与过这个问题的片段中。我只是用最少量的代码在全新的Spring Boot(1.3.2。)设置中尝试了它,并且在我从其他域POST'时正确添加了CORS头。你使用什么版本的Boot,从你的用户代理那里“发布”你的请求?你的项目中的其他依赖项可能会覆盖你的Cors设置(或者在控制器上更明确的配置?)? – sthzg
我使用Spring Boot 1.3.0.RELEASE和用户代理是Firefox。实际上它是重定向到其他弹簧引导应用程序的其他端点的Api网关应用程序。我使用netflix的Zuul。 – nurgasemetey