官方推荐方式:

http://spring.io/blog/2015/06/08/cors-support-in-spring-framework

方式1:

    $.ajax({  //前台:常规写法。注意:如果用注解dataType别写成了JSONP 要写成JSON 两都有区别。一个是协议一个是数据格式(个人理解)
url:'http://localhost:8082/cors/corsTest.do',
data:{},
dataType:'json',
type:'post',
success:function(data){
alert(data.abc);
}
})
// 后台代码 。。。 添加注解即可。CrossOrigin(这里可能配置多个地址。不配置默认全部 可以配置到controller上面。也可以配置方法上。都配置他会自动合并)
   @SuppressWarnings("unchecked")
@RequestMapping(value="/corsTest",method = RequestMethod.POST)
@ResponseBody
@CrossOrigin
public Object corsTest(){
Map map = new HashMap();
map.put("abc", 123);
return map;
} 方式2: $.ajax({ //不采用注解方法,手动拼一个callback字符串。
url:'http://localhost:8082/cors/corsTest1.do',
data:{},
async:false,
dataType:'jsonp',
jsonpCallback:"callback",
type:'post',
success:function(data){
alert(data.abc);
}
})     
     后台代码:
  @RequestMapping(value="/corsTest1")
@ResponseBody
public Object corsTest1(){
Map map = new HashMap();
map.put("abc", 456);
return "callback("+JSON.toJSONString(map)+")";
} 方式3: 即不采用注解也不采用手动拼接字符串的方式 。就是用拦截器方式; public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response; // 跨域
String origin = httpRequest.getHeader("Origin");
if (origin == null) {
httpResponse.addHeader("Access-Control-Allow-Origin", "*");
} else {
httpResponse.addHeader("Access-Control-Allow-Origin", origin);
}
httpResponse.addHeader("Access-Control-Allow-Headers", "Origin, x-requested-with, Content-Type, Accept,X-Cookie");
httpResponse.addHeader("Access-Control-Allow-Credentials", "true");
httpResponse.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");
if ( httpRequest.getMethod().equals("OPTIONS") ) {
httpResponse.setStatus(HttpServletResponse.SC_OK);
return;
}
chain.doFilter(request, response);
} catch (Exception e) {
logger.error("Exception in crossFilter", e);
throw e;
}
}
然后在web.xml配置一下就行了。
<filter>
<filter-name>crossDemoFilter</filter-name>
<filter-class>com.shanreal.filter.crossFilter</filter-class>
</filter> <filter-mapping>
<filter-name>crossDemoFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
还有一种就是通过nginx配置。学习中

spring mvc 跨域问题。。。解决的更多相关文章

  1. 关于Spring MVC跨域

    1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ...

  2. spring mvc跨域设置(全局)

    //--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ...

  3. spring mvc 跨域请求处理——spring 4.2 以上

    Controller method CORS configuration You can add to your @RequestMapping annotated handler method a  ...

  4. spring mvc跨域(ajax post json)--filter方案

    @RequestMapping(value = "/login.do",method = RequestMethod.POST) public Message login(Http ...

  5. spring mvc跨域(post)--filter方案

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...

  6. Ajax跨域访问解决办法

    方法1. jsonp实现ajax跨域访问示例 jsp代码: <body> <input type="button" onclick="testJsonp ...

  7. spring boot跨域设置

    定义 跨域是指从一个域名的网页去请求另一个域名的资源 跨域背景 限制原因 如果一个网页可以随意地访问另外一个网站的资源,那么就有可能在客户完全不知情的情况下出现安全问题 为什么要跨域 公司内部有多个不 ...

  8. spring boot跨域请求访问配置以及spring security中配置失效的原理解析

    一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[orig ...

  9. SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑

    前言 最近在做项目的时候,基于前后端分离的权限管理系统,后台使用 Spring Security 作为权限控制管理, 然后在前端接口访问时候涉及到跨域,但我怎么配置跨域也没有生效,这里有一个坑,在使用 ...

随机推荐

  1. Hystrix 常用属性配置

    配置参数 默认值 说明 命令-执行属性配置 hystrix.command.default.execution.isolation.strategy THREAD 配置隔离策略,有效值 THREAD, ...

  2. 排序算法<No.5>【堆排序】

    算法,是系统软件开发,甚至是搞软件的技术人士的核心竞争力,这一点,我坚信不疑.践行算法实践,已经有一段时间没有practise了,今天来一个相对麻烦点的,堆排序. 1. 什么是堆(Heap) 这里说的 ...

  3. ML: 聚类算法-K均值聚类

    基于划分方法聚类算法R包: K-均值聚类(K-means)                   stats::kmeans().fpc::kmeansruns() K-中心点聚类(K-Medoids) ...

  4. 关于AXI_Quad_SPI的寄存器配置

    关于AXI_Quad_SPI的寄存器配置 1.核初始化配置 首先是: 40:0000_000A 1C:8000_0000 28:0000_0004 2.命令与dummy_data 60:000001E ...

  5. SPI Flash Memory 芯片手册阅读

    SPI Flash Memory 芯片手册阅读 信息来源

  6. CentOS 7 firewalld vsftpd开放端口

    开放FTP端口的方法: 暂时开放 ftp 服务 firewall-cmd --add-service=ftp 1 永久开放 ftp 服务 firewall-cmd --add-service=ftp ...

  7. 关于使用MAPVIEWOFFILE大文件的读写(DELPHI版)

    unit filemap; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  8. STL基础--String

    String 构造 string s1("Hello"); string s2("Hello", 3); //s2: Hel string s3(s1, 2); ...

  9. C#实现mongodb自增列的使用

    创建一个集合存放_id db.createCollection("counters") 加入需要自增的字段 { "_id":"productid&qu ...

  10. Flume监听文件目录sink至hdfs配置

    一:flume介绍 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能 ...