返回> 网站首页
springboot跨域访问
yoours2021-07-01 21:57:17
简介一边听听音乐,一边写写文章。
一、跨域访问
只实现了cookie跨域访问
二、拦截器
public class CorsInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
crossDomain(request, response);
return true;
}
public void crossDomain(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "http://soskp.com,http://www.soskp.com,http://wx.soskp.com");
response.setHeader("Access-Control-Allow-Methods", "POST,GET");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token");
// 如果要把Cookie发到服务器,需要指定Access-Control-Allow-Credentials字段为true
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Expose-Headers", "*");
}
}