欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 08:20:16
上次给一个网站写网站  前后端分离 最后跪在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跨域请求处理方法实例详解,希望对大家有所帮助