spring的CORS

先来看一下CORS(Cross-Origin Resource Sharing)是什么东西:https://spring.io/understanding/CORS,这篇文章做了详细的介绍.看这篇文章之前再先了解一下same-origin policy术语.可参考:https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy.这里介绍orgin的定义时提到:Two pages have the same origin if the protocol, port (if one is specified), and host are the same for both pages.(如果两个页面的协议,端口,主机都相同时,那么这两个页面就有相同的源)从一个页面向另一个不同源的页面发出请求,浏览器默认是阻止JS代码的.CORS是一项可以放宽同源策略,允许网页的JS使用来自不同源提供的REST API的技术.
如果请求可能影响用户数据,可能就要先预检CORS请求.再根据这个响应来提交请求.

再看一下实践(spring 4.2+)spring的参考文档pdf或https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
全局CORS配置是最简单的;如果要使用@CrossOrigin,经我实践,methods属性是必须配置的(默认值应该不是所有方法);

如果提示:error: annotation CrossOrigin is missing value for the attribute
就更新jdk7到最新版或更新到jdk8最新版.我在java version “1.7.0_67”出现了这个错.上面链接帖子的评论也有提到这个错

你可能感兴趣的:(跨域,CORS,Cross-Orig)