JS跨域访问 通过 Spring mvc 拦截器修改返回值 HttpServletResponse

2020-06-24 社会 109阅读
给你个例子,controller需要支持跨域的方法:
public Map index(ServletResponse res){
HttpServletResponse response = (HttpServletResponse)res;
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST,GET,PUT,DELETE,OPTIONS");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With,token");
response.addHeader("Access-Control-Max-Age", "600000");
//TODO
...
}
这样就支持跨域了,但是这种方式不怎么安全,"Access-Control-Allow-Origin"后面的*代表支持所有域名。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com