亲宝软件园·资讯

展开

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

人气:0
想了解Springmvc ajax跨域请求处理方法实例详解的相关内容吗,在本文为您仔细讲解springmvc ajax 跨域请求的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,mvc,ajax,跨域请求,ajax跨域请求,下面大家一起来学习吧。

上次给一个网站写网站  前后端分离 最后跪在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里面配置一下路径

<mvc:interceptors>
<!--过滤所有请求,处理跨域请求问题 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.util.CommonInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

 这样就可以了  不过有篇博客说针对简单跨域没问题。但是针对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里面设置一下

<filter>
  <filter-name>cors</filter-name>
  <filter-class>cn.***.filter.CrossFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>cors</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

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

xml里面配置

  <mvc:cors>
    <mvc:mapping path="/**" allowed-origins="*" allow-credentials="true" max-age="1800" allowed-methods="GET,POST,OPTIONS"/>
  </mvc:cors>

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);
  }
}

好了,代码到此结束!

总结

以上所述是小编给大家介绍的Springmvc ajax跨域请求处理方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论