一聚教程网:一个值得你收藏的教程网站

热门教程

Springmvc ajax跨域请求处理方法实例详解

时间:2022-06-29 01:12:34 编辑:袖梨 来源:一聚教程网

上次给一个网站写网站  前后端分离 最后跪在ajax跨域上面了  自己在网上找了个方法  亲试可用  记录一下

写一个类  继承HandlerInterceptorAdapter

package com.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class CommonInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept");
response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否允许浏览器携带用户身份信息(cookie)
return true;
}
}

然后在xml里面配置一下路径







 这样就可以了  不过有篇博客说针对简单跨域没问题。但是针对post+json请求却失败,提示跨域失败。所以下面记录一下他的解决方式

建立一个类  继承 OncePerRequestFilter

public class CrossFilter extends OncePerRequestFilter {
  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
      // CORS "pre-flight" request
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
      response.addHeader("Access-Control-Allow-Headers", "Content-Type");
      response.addHeader("Access-Control-Max-Age", "1800");//30 min
    }
    filterChain.doFilter(request, response);
  }
}

web.xml里面设置一下


  cors
  cn.***.filter.CrossFilter


  cors
  /*

然后就可以了  上述方式为spring3.0版本  如果是4.0版本可以用下面方式(无测)

xml里面配置

  
    
  

PS:使用SpringMVC解决Ajax跨域问题

package com.mengyao.system.filter;
 import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
/**
 * CORSFilter 解决跨域问题
 * @author mengyao
 *
 */
@Component
public class InitCORSFilter extends OncePerRequestFilter {
  private Logger logger = LoggerFactory.getLogger(InitCORSFilter.class);
  public InitCORSFilter() {
    logger.info("==== 初始化系统允许跨域请求 ====");
  }
  /**
   * 解决跨域:Access-Control-Allow-Origin,值为*表示服务器端允许任意Domain访问请求
   */
  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {
    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
      response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with, sid, mycustom, smuser");
      response.addHeader("Access-Control-Max-Age", "1800");//30 min
    }
    filterChain.doFilter(request, response);
  }
}

好了,代码到此结束!

热门栏目