【发布时间】:2020-06-26 06:13:58
【问题描述】:
我尝试在安全上下文中对 Spring Rest Web 服务 API 进行 ajax 跨域发布调用。 从 Jquery 我无法设置 contentType 属性 cos 然后我遇到了安全上下文的问题。 但是如果没有 spring 的 contentType,我会收到以下响应:415 (Unsupported Media Type)
弹簧控制器:
@RequestMapping(value = "/all", method = RequestMethod.POST)
@PreAuthorize("hasAnyAuthority('PROF1','SUDO')")
jquery:
function getAllUsers(){
var obj = {"limit":10,"page":0};
$.ajax({
url:"https://webServerSite/myService/api/v1/user/all",
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
data:obj,
type: "post",
success:function(json){
var str = JSON.stringify(json);
console.log(str);
},
error:function(xhr, status, error) {
alert(xhr.responseText);
}
});
}
有办法在 Spring 上禁用 contentType 检查吗? 我所有的数据都是 json,我希望将其设置为默认值,以避免内容类型标头检查。 我尝试定义一个自定义消息转换器并使用以下代码不起作用:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//set path extension to true
configurer.favorPathExtension(true).
//set favor parameter to false
favorParameter(false).
//ignore the accept headers
ignoreAcceptHeader(true).
//dont use Java Activation Framework since we are manually specifying the mediatypes required below
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML).
mediaType("json", MediaType.APPLICATION_JSON);
}
谢谢
【问题讨论】:
标签: jquery ajax spring-boot rest cross-domain