热门在SpringMVC中设置所有响应标头?

时间:2016-07-14 01:59:42

标签: spring-mvc cors

我正在寻求解决CORS问题。所以我试着用:

response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

有效。但我必须到处写。有没有办法简化它?

@ResponseBody
@RequestMapping(value = "/manage/getEncloser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String getEncloser(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
    // TODO CORS    
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    return JSONObject.toJSONString(encloserService.list(filter));
}

1 个答案:

答案 0 :(得分:0)

传统方式,你可以编写一个Servlet过滤器来做到这一点。以下是使用过滤器解决CORS问题的示例。请参阅以下链接以供参考: https://gist.github.com/zeroows/80bbe076d15cb8a4f0ad

顺便说一下,Spring Framework提供了几种处理CORS的方法。你可以看看下面的链接: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework