SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月14日

http://www.cnblogs.com/fanshuyao/

一、SpringMvc跨域支持

从Spring MVC 4.2 开始增加支持跨域访问

二、使用方法

1、某个方法支持跨域访问

在方法上增加@CrossOrigin注解,如下:

  1. @RequestMapping("/crossDomain2")
  2. @ResponseBody
  3. @CrossOrigin
  4. public String crossDomain2(HttpServletRequest req, HttpServletResponse res, String name){
  5. ……
  6. ……
  7. }

其中@CrossOrigin中的2个参数:

origins  : 允许可访问的域列表

  1. List of allowed origins, e.g. "http://domain1.com".
  2. These values are placed in the Access-Control-Allow-Origin header of both the pre-flight response and the actual response. "*" means that all origins are allowed.
  3. If undefined, all origins are allowed.

 maxAge:飞行前响应的缓存持续时间的最大年龄(以秒为单位)。

  1. The maximum age (in seconds) of the cache duration for pre-flight responses.
  2. This property controls the value of the Access-Control-Max-Age header in the pre-flight response.
  3. Setting this to a reasonable value can reduce the number of pre-flight request/response interactions required by the browser. A negative value means undefined.
  4. If undefined, max age is set to 1800 seconds (i.e., 30 minutes).

2、整个Controller都支持跨域访问,在类上面加上注解@CrossOrigin,如下:

  1. @Controller
  2. @CrossOrigin
  3. public class TestController {
  4. ……
  5. ……
  6. }

3、自定义规则支持全局跨域访问,在spring-mvc.xml文件中配置映射路径,如下:

  1. <mvc:cors>
  2. <mvc:mapping path="/cross/*"/>
  3. </mvc:cors>

上面表示有/cross/路径的请求都支持跨域访问,也可以增加其它的,如下:

  1. <mvc:cors>
  2. <mvc:mapping path="/cross/*" allowed-origins="" max-age="2500"/>
  3. <mvc:mapping path="/domain/*"/>
  4. </mvc:cors>

请求路径有/cross/,方法示例如下:

  1. @RequestMapping("/cross/crossDomain")
  2. @ResponseBody
  3. public String crossDomain(HttpServletRequest req, HttpServletResponse res, String name){
  4. ……
  5. ……
  6. }

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月14日

http://www.cnblogs.com/fanshuyao/

SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域的更多相关文章

  1. SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域[转]

    SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域 原文地址:https://www.cnblogs.com/fanshuyao/p/716847 ...

  2. springmvc支持跨域的请求(复制)

    Spring MVC 新增跨域支持 发表于2017/5/8 22:01:24  48人阅读 分类: SpringMVC Spring MVC 4.2 增加 CORS 支持 跨站 HTTP 请求(Cro ...

  3. spring boot / cloud (六) 开启CORS跨域访问

    spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...

  4. Spring Boot Web应用开发 CORS 跨域请求支持:

    Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...

  5. Spring Boot中通过CORS解决跨域问题

    今天和小伙伴们来聊一聊通过CORS解决跨域问题. 同源策略 很多人对跨域有一种误解,以为这是前端的事,和后端没关系,其实不是这样的,说到跨域,就不得不说说浏览器的同源策略. 同源策略是由Netscap ...

  6. spring boot + spring security +前后端分离【跨域】配置 + ajax的json传输数据

    1.前言 网上各个社区的博客参差不齐 ,给初学者很大的困扰 , 我琢磨了一天一夜,到各个社区找资料,然后不断测试,遇到各种坑,一言难尽啊,要么源码只有一部分,要么直接报错... 最后实在不行,直接去看 ...

  7. Spring Boot 最简单的解决跨域问题

    跨域问题(CORS) CORS全称Cross-Origin Resource Sharing,意为跨域资源共享.当一个资源去访问另一个不同域名或者同域名不同端口的资源时,就会发出跨域请求.如果此时另一 ...

  8. Cross-Origin跨域问题

    为什么会跨域,要先了解浏览器的同源策略SOP(Same Orign Policy)  https://segmentfault.com/a/1190000015597029 同源: 如果两个页面的协议 ...

  9. 前端Js跨域方法汇总—剪不断,理还乱,是跨域

    1.通过jsonp跨域2.通过修改document.domain来跨子域(iframe)3.隐藏的iframe+window.name跨域4.iframe+跨文档消息传递(XDM)5.跨域资源共享 C ...

随机推荐

  1. Java集合类从属关系

    Java的集合分为了四类:List Set Queue Map,每类都有不同的实现,有基于数组实现的,有基于链表实现的,有基于xx树实现的,不同的实现虽在功能上可以相互替代但都有各自的应用场景,如基于 ...

  2. hadoop 2.7.3 集群安装

    三台虚拟机,centos6.5 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 :: loca ...

  3. springboot 1.5.2 thymeleaf 标签未关闭异常解决办法

    org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="login&qu ...

  4. Python爬虫学习(二) ——————爬取前程无忧招聘信息并写入excel

    作为一名Pythoner,相信大家对Python的就业前景或多或少会有一些关注.索性我们就写一个爬虫去获取一些我们需要的信息,今天我们要爬取的是前程无忧!说干就干!进入到前程无忧的官网,输入关键字&q ...

  5. web前端面试集锦(自己搜集的,如有错误请不吝赐教)

    css 1 浏览器兼容性 CSS hack(针对IE6-,IE7,IE8,IE9以及其他浏览器) ‘ * ’ : 所有的IE浏览器都能识别 说明:在标准模式中 “-″减号是IE6专有的hack “\9 ...

  6. 学习笔记TF020:序列标注、手写小写字母OCR数据集、双向RNN

    序列标注(sequence labelling),输入序列每一帧预测一个类别.OCR(Optical Character Recognition 光学字符识别). MIT口语系统研究组Rob Kass ...

  7. jdbc的配置及jdbc连接常用数据库(mysql、sqlserver、Oracle)

    1.连接SQL Server数据库 import java.sql.*; publicclassMain{publicstaticvoid main(String[] args){String dri ...

  8. Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' ...

  9. 【Windows 10 应用开发】自定义快捷键

    上一篇鸟文中,老周通过史无前例的代码向各位 demo 了访问键的用法(即 Alt + 某某).不过,大伙伴们一定会发现,访问键毕竟限制较大,不太灵活,也不好发挥,于是就需要自定义快捷键了. 其实,自定 ...

  10. 单机部署 kubernets 方法汇总

    #minikube : 可以方便的在本机用虚拟机创建一个开箱即用的Kubernetes集群 #kubeadm : 可以自动化的将多台Ubuntu或者CentOS主机组建成集群 #nanokube,ki ...